美文网首页
flutter中使用命名路由跳转时传参(pushNamed)并在

flutter中使用命名路由跳转时传参(pushNamed)并在

作者: _敏讷 | 来源:发表于2019-12-25 23:47 被阅读0次

    当我使用routes属性定义命名路由,并通过下列方法进行携带arguments参数进行跳转

    Navigator.pushNamed(
          context,
          routeName,
          arguments: <args>,
    );
    

    然而,当我在initState中通过下列方式接收参数时,debug console中出现了报错

    @override
    void initState() {
      args = ModalRoute.of(context).settings.arguments;
    }
    

    报错内容如下

    20:49:44.129 4 info flutter.tools I/flutter ( 2680): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
    20:49:44.129 5 info flutter.tools I/flutter ( 2680): The following assertion was thrown building Builder:
    20:49:44.129 6 info flutter.tools I/flutter ( 2680): inheritFromWidgetOfExactType(_ModalScopeStatus) or inheritFromElement() was called before
    20:49:44.130 7 info flutter.tools I/flutter ( 2680): _CourseCohortScreenState.initState() completed.
    20:49:44.130 8 info flutter.tools I/flutter ( 2680): When an inherited widget changes, for example if the value of Theme.of() changes, its dependent
    20:49:44.131 9 info flutter.tools I/flutter ( 2680): widgets are rebuilt. If the dependent widget's reference to the inherited widget is in a constructor
    20:49:44.131 10 info flutter.tools I/flutter ( 2680): or an initState() method, then the rebuilt dependent widget will not reflect the changes in the
    20:49:44.131 11 info flutter.tools I/flutter ( 2680): inherited widget.
    20:49:44.138 12 info flutter.tools I/flutter ( 2680): Typically references to inherited widgets should occur in widget build() methods. Alternatively,
    20:49:44.138 13 info flutter.tools I/flutter ( 2680): initialization based on inherited widgets can be placed in the didChangeDependencies method, which
    20:49:44.138 14 info flutter.tools I/flutter ( 2680): is called after initState and whenever the dependencies change thereafter.
    

    后来google之后找到了解决办法(虽然目前的状态还是知其然不知其所以然)

    1. 配置落地页的构建和参数调用逻辑:
    //  login_page.dart
    class LoginPage extends StatefulWidget {
      final String share_id;
      LoginPage({ Key key,  this.share_id }) : super(key: key);
    
      @override
      State<StatefulWidget> createState() => LoginPageState();
    }
    
    class LoginPageState extends  State<LoginPage> {
    
      @override
      void initState() {
        super.initState();
        getUserInfo(widget.user_id);    // 使用userId
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(),
          body: Center(
            child: Text(
              'hello ${widget.user_id}',
              textScaleFactor: 5.0,
            ),
          ),
        );
      }
    }
    
    1. 在MaterialApp中使用onGenerateRoute属性来定义命名路由,在需要传入参数的Widget页面定义初始化变量,并在路由定义时传入变量:
    // main.dart
    import './pages/login_page.dart'    // 引用LoginPage
    ...
    onGenerateRoute: (RouteSettings settings) {
      print('build route for ${settings.name}');
      var routes = <String, WidgetBuilder>{
        "login": (ctx) => LoginPage(settings.arguments),
        "other": (ctx) => SomeWidget(),
      };
      WidgetBuilder builder = routes[settings.name];
      return MaterialPageRoute(builder: (ctx) => builder(ctx));
    },
    

    3此时便可以调用相同的方法来实现路由跳转,并传入参数

    Navigator.of(context).pushNamed("login", arguments: "IDXXXX");    
    

    相关文章

      网友评论

          本文标题:flutter中使用命名路由跳转时传参(pushNamed)并在

          本文链接:https://www.haomeiwen.com/subject/wmsqoctx.html