简体   繁体   English

flutter 路线显示找不到路线 RouteSettings 的生成器

[英]flutter routes showing Could not find a generator for route RouteSettings

When I am building a flutter app there is three Screens, Screen A, Screen B, Screen C.当我构建 flutter 应用程序时,有三个屏幕,屏幕 A、屏幕 B、屏幕 C。 When clicking on button it redirects to next screen.单击按钮时,它会重定向到下一个屏幕。 So it is redirecting A to B. But not redirecting B to C.所以它将 A 重定向到 B。但没有将 B 重定向到 C。 I don't know the reason.我不知道原因。 It showing error.它显示错误。 It showing the error is它显示错误是

Could not find a generator for route RouteSettings("/extractArguments1", Instance of 'ScreenArguments') in the _WidgetsAppState.

My code shown below我的代码如下所示

    import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      routes: {
        '/extractArguments': (context) => ExtractArgumentsScreen(),
        '/extractArguments1': (context) => ExtractArgumentsScreen1(),
      },
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {




  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(

        title: Text("A"),
      ),
      body: Center(
        child: new RaisedButton(onPressed: (){
          Navigator.pushNamed(context, "/extractArguments",arguments: ScreenArguments("title", "message"));
        },
        child: new Text("Click"),),

      ),

    );
  }
}


class ScreenArguments {
  final String title;
  final String message;

  ScreenArguments(this.title, this.message);
}

class ExtractArgumentsScreen extends StatelessWidget{

ExtractArgumentsScreen({
    Key key,
    @required this.title,
    @required this.message,
  }): super(key: key);

  final String title;
  final String message;

  @override
  Widget build(BuildContext context) {

    final ScreenArguments args = ModalRoute.of(context).settings.arguments;


    // return new Scaffold(
    //   appBar: new AppBar(title: new Text('args.title')),
    //     body: new Center(child: new Text('args.message'),),
    // );

    return MaterialApp(
      title: 'Flutter Demo',

      home: MyHomePage1(title: 'Flutter Demo Home Page'),
    );


  }



}


class MyHomePage1 extends StatefulWidget {
  MyHomePage1({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePage1State createState() => _MyHomePage1State();
}

class _MyHomePage1State extends State<MyHomePage1> {




  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(

        title: Text("B"),
      ),
      body: Center(
        child: new RaisedButton(onPressed: (){
          Navigator.pushNamed(context, "/extractArguments1",arguments: ScreenArguments("title", "message"));
        },
        child: new Text("Click"),),

      ),

    );
  }
}

class ExtractArgumentsScreen1 extends StatelessWidget{

ExtractArgumentsScreen1({
    Key key,
    @required this.title,
    @required this.message,
  }): super(key: key);

  final String title;
  final String message;

  @override
  Widget build(BuildContext context) {

    final ScreenArguments args = ModalRoute.of(context).settings.arguments;


    // return new Scaffold(
    //   appBar: new AppBar(title: new Text('args.title')),
    //     body: new Center(child: new Text('args.message'),),
    // );

    return MaterialApp(
      title: 'Flutter Demo',

      home: MyHomePage2(title: 'Flutter Demo Home Page'),
    );


  }



}


class MyHomePage2 extends StatefulWidget {
  MyHomePage2({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePage2State createState() => _MyHomePage2State();
}

class _MyHomePage2State extends State<MyHomePage2> {




  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(

        title: Text("C"),
      ),
      body: Center(
        child: new RaisedButton(onPressed: (){
      //   Navigator.pushNamed(context, "/extractArguments1",arguments: ScreenArguments("title", "message"));
        },
        child: new Text("Click"),),

      ),

    );
  }
}

And the complete error is完整的错误是

     The following assertion was thrown while handling a gesture:
  I/flutter (23854): Could not find a generator for route RouteSettings("/extractArguments1", Instance of
  I/flutter (23854): 'ScreenArguments') in the _WidgetsAppState.
  I/flutter (23854): Generators for routes are searched for in the following order:
  I/flutter (23854):  1. For the "/" route, the "home" property, if non-null, is used.
  I/flutter (23854):  2. Otherwise, the "routes" table is used, if it has an entry for the route.
  I/flutter (23854):  3. Otherwise, onGenerateRoute is called. It should return a non-null value for any valid route not
  I/flutter (23854): handled by "home" and "routes".
  I/flutter (23854):  4. Finally if all else fails onUnknownRoute is called.
  I/flutter (23854): Unfortunately, onUnknownRoute was not set.

Please help me to find the issue.请帮我找出问题所在。

The problem is when you are calling:问题是当你打电话时:

 Navigator.pushNamed(context, "/extractArguments1",arguments: ScreenArguments("title", "message"));

You are passing the arguments in a named route, but you are nowhere accepting the arguments.您在命名路线中通过 arguments,但您无处接受 arguments。 You need to extract the arguments inside an onGenerateRoute() function and pass them to a widget.您需要在onGenerateRoute() function 中提取 arguments 并将它们传递给小部件。 The onGenerateRoute() function creates the correct route based on the given RouteSettings. onGenerateRoute() function 根据给定的 RouteSettings 创建正确的路由。 So what you can do is add onGenerateRoute() function inside your material app as follows:所以你可以做的是在你的材料应用程序中添加 onGenerateRoute() function 如下:

 onGenerateRoute: (settings) {

    if (settings.name == "/extractArguments1") {

      final ScreenArguments args = settings.arguments;

      // Extract the required data from the arguments and
      // pass the data to the correct screen.
      return MaterialPageRoute(
        builder: (context) {
          return ExtractArgumentsScreen1(
            title: args.title,
            message: args.message,
          );
        },
      );
    }
  },

This function will handle the arguments correctly pass them to respective screen screen.此 function 将处理 arguments 正确地将它们传递到相应的屏幕屏幕。 For reference see official documentation .参考见官方文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 在 _WidgetsAppState 中找不到路由 RouteSettings(“/NextScreen”, null) 的生成器。? - Flutter Could not find a generator for route RouteSettings(“/NextScreen”, null) in the _WidgetsAppState.? Flutter 导航器和路由在单独的文件中显示错误:在 _Widget 中找不到路由 RouteSettings("/details", null) 的生成器 - Flutter Navigator and Routing in separate files showing error: Could not find a generator for route RouteSettings("/details", null) in the _Widget Flutter:在 _WidgetsAppState 中找不到路由 RouteSettings(&quot;/HomePage&quot;, null) 的生成器 - Flutter : Could not find a generator for route RouteSettings("/HomePage", null) in the _WidgetsAppState 在 _WidgetsAppState 中找不到路由 RouteSettings(“/”, null) 的生成器 - Could not find a generator for route RouteSettings(“/”, null) in the _WidgetsAppState flutter: 在 I/flutter (26461): _WidgetsAppState 中找不到路由 RouteSettings(&quot;/HairServices-product-detail&quot;, 1) 的生成器 - flutter: Could not find a generator for route RouteSettings(“/HairServices-product-detail”, 1) in the I/flutter (26461): _WidgetsAppState 在 _WidgetsAppState 中找不到路由 RouteSettings("todoscreen", Instance of 'ScreenArguments') 的生成器。 FLUTTER - Could not find a generator for route RouteSettings("todoscreen", Instance of 'ScreenArguments') in the _WidgetsAppState. FLUTTER 如何解决此错误在 flutter 中找不到路由 RouteSettings(“Welcome”, null) 的生成器 - how to solve this error Could not find a generator for route RouteSettings(“Welcome”, null) in flutter 在 _WidgetsAppState 中找不到路由 RouteSettings("org_Item", null) 的生成器 - could not find a generator for route RouteSettings("org_Item", null) in the _WidgetsAppState 在 _CustomTabViewState 中找不到路由 RouteSettings("map-screen", null) 的生成器 - Could not find a generator for route RouteSettings("map-screen", null) in the _CustomTabViewState 在 _WidgetsAppState 中找不到路由 RouteSettings(&quot;SecondScreens&quot;, null) 的生成器 - Could not find a generator for route RouteSettings("SecondScreens", null) in the _WidgetsAppState
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM