推送到下一个屏幕时无法在 Navigator.pushNamed 中找到 'arguments'

Cannot find the 'arguments' in Navigator.pushNamed when pushing to next screen

我正在尝试将注册屏幕移至 OTP 屏幕。从注册屏幕我需要将电子邮件 ID 传递到 OTP 屏幕。现在,我在下面使用,但无法解析参数:其中的参数。

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

搜索了那么多教程,他们在那里给出了使用参数的方法:(但我找不到:(

下面是一个证明您的要求的简单示例:

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),
      ),
    );
  }
}