推送到下一个屏幕时,无法在Navigator.pushNamed中找到“参数”

时间:2019-07-10 09:44:15

标签: flutter dart navigator

我正在尝试将注册屏幕移至OTP屏幕。从“注册”屏幕,我需要将电子邮件ID传递到OTP屏幕。现在,我在下面使用,但是不能解析参数:

Navigator.pushNamed(context, Routes.ROUTE_OTP,arguments:{"id": 'email'});

搜索了很多他们在其中使用参数的教程:(但是我不好,我找不到:(

1 个答案:

答案 0 :(得分:1)

一个简单的示例说明您的要求:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      onGenerateRoute: (settings) {
        WidgetBuilder builder;
        Map arguments = settings.arguments;

        switch (settings.name) {
          case '/':
            builder = (
              BuildContext _,
            ) =>
                SignUp();
            break;
          case '/otp':
            builder = (
              BuildContext _,
            ) =>
                Otp(id: arguments["id"]);
            break;
          default:
            return null;
        }
        return MaterialPageRoute(builder: builder, settings: settings);
      },
    );
  }
}

class SignUp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Signup"),
      ),
      body: Center(
        child: FlatButton(
            onPressed: () {
              Navigator.of(context).pushNamed(
                "/otp",
                arguments: {
                  "id": "email@email.com",
                },
              );
            },
            child: Text("SEND OTP")),
      ),
    );
  }
}

class Otp extends StatelessWidget {
  final String id;

  Otp({this.id});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("OTP"),
      ),
      body: Center(
        child: Text(id),
      ),
    );
  }
}

gif