Flutter-使用pushName获取数据 [英] Flutter - Get data using pushName

查看:123
本文介绍了Flutter-使用pushName获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用pushName发送数据.然后,我尝试获取此数据以显示在Toast消息中.

PushName

  Navigator.pushNamed(语境,'/航海家',参数:< String,String> {'instalation':widget.instalation,'消息':DemoLocalizations.of(上下文).text('取消消息')+" +widget.datameterValue.toString(),},); 

尝试检索数据

 类导航扩展了StatefulWidget {最终的ConnectionPage参数;导航({Key key,this.message,this.instalation,this.args}):super(key:key);}类别_NavigationState扩展了State< Navigation>{无效的initState(){super.initState();打印(widget.args);//空值最后的便条= SnackBar(持续时间:持续时间(秒:5),内容:文本(widget.args.messsage +'.',textAlign:TextAlign.center),backgroundColor:Colors.red [700],);key.currentState.showSnackBar(snackBar);}} 

问题:返回null.

所以:使用pushName获取数据的正确方法是什么?在文档中显示了如何在脚手架内部

完整代码

 导入'package:flutter/material.dart';void main()=>runApp(MyApp());MyApp类扩展了StatelessWidget {@override窗口小部件build(BuildContext context){返回MaterialApp(//提供一个处理命名路由的函数.使用此功能可以//识别要推送的命名路由,并创建正确的路由//屏幕.路线:{'/extractArguments':(上下文)=>ExtractArgumentsScreen(),},onGenerateRoute :(设置){//如果您按下PassArguments路线如果(settings.name == PassArgumentsScreen.routeName){//将参数强制转换为正确的类型:ScreenArguments.final ScreenArguments args = settings.arguments;//然后,从参数中提取所需的数据,然后//将数据传递到正确的屏幕.返回MaterialPageRoute(生成器:(上下文){返回PassArgumentsScreen(标题:args.title,讯息:args.message,);},);}},标题:带参数导航",主页:HomeScreen(),);}}类HomeScreen扩展了StatelessWidget {@override窗口小部件build(BuildContext context){返回脚手架(appBar:AppBar(标题:文字(主屏幕"),),身体:中心(子:列(mainAxisAlignment:MainAxisAlignment.center,子代:< Widget> [//导航到该路由的按钮.命名路线//本身提取参数.加高按钮(子级:Text(导航到提取参数的屏幕"),onPressed:(){//当用户点击按钮时,导航至特定路线//,并提供参数作为RouteSettings的一部分.Navigator.pushNamed(语境,ExtractArgumentsScreen.routeName,参数:{'instalation':"123","message":"456"},);/*Navigator.push(语境,MaterialPageRoute(生成器:(上下文)=>ExtractArgumentsScreen(),//将参数作为RouteSettings的一部分传递.这//ExtractArgumentScreen从这些参数中读取参数//设置.设置:RouteSettings(参数:{'instalation':"123","message":"456"},),),); */},),//导航到命名路线的按钮.对于此路线,提取//onGenerateRoute函数中的参数并将其传递//到屏幕.加高按钮(子级:Text(导航到接受参数的名称"),onPressed:(){//当用户点击按钮时,导航至命名路线//并提供参数作为可选参数.Navigator.pushNamed(语境,PassArgumentsScreen.routeName,参数:ScreenArguments(接受参数屏幕",此消息是在onGenerateRoute函数中提取的.",),);},),],),),);}}//一个小部件,该小部件从ModalRoute中提取必要的参数.类ExtractArgumentsScreen扩展了StatefulWidget {静态const routeName ='/extractArguments';@override_ExtractArgumentsScreenState createState()=>_ExtractArgumentsScreenState();}类_ExtractArgumentsScreenState扩展了State< ExtractArgumentsScreen>{最终的GlobalKey< ScaffoldState>键=新的GlobalKey< ScaffoldState>();最后的便条= SnackBar(持续时间:持续时间(秒:5),内容:Text("message" +'.',textAlign:TextAlign.center),backgroundColor:Colors.red [700],);@override无效的initState(){super.initState();WidgetsBinding.instance.addPostFrameCallback((_){最终的路线Args1 =ModalRoute.of(context).settings.arguments as Map< String,String> ;;最终安装= routeArgs1 ['instalation'];最终消息= routeArgs1 ['message'];print('instalation $ {instalation}');print('message $ {message}');key.currentState.showSnackBar(SnackBar(content:Text(message)));});}@overridevoid didChangeDependencies(){super.didChangeDependencies();}@override窗口小部件build(BuildContext context){//从当前的ModalRoute设置中提取参数并进行强制转换//将它们作为ScreenArguments.最终路线Args =ModalRoute.of(context).settings.arguments as Map< String,String> ;;最终安装= routeArgs ['instalation'];最终消息= routeArgs ['message'];返回脚手架(键:键,appBar:AppBar(title:文字('$ {routeArgs ['code']}'),),正文:列(子代:< Widget> [中心(子级:Text('instalation $ {instalation}'),),加高按钮(onPressed:(){key.currentState.showSnackBar(snackBar);},),],),);}}//一个通过构造函数接受必要参数的Widget.PassArgumentsScreen类扩​​展了StatelessWidget {静态常量routeName ='/passArguments';最终的字符串标题;最终的字符串消息;//此小部件接受参数作为构造函数参数.它不是//从ModalRoute中提取参数.////参数是通过提供给//MaterialApp小部件.const PassArgumentsScreen({关键@需要this.title,@需要this.message,}):super(key:key);@override窗口小部件build(BuildContext context){返回脚手架(appBar:AppBar(标题:文字(标题),),身体:中心(子代:文字(消息),),);}}//您可以将任何对象传递给arguments参数.在这个例子中//创建一个同时包含可自定义标题和消息的类.类ScreenArguments {最终的字符串标题;最终的字符串消息;ScreenArguments(this.title,this.message);} 

I´m trying to send data with pushName. Then i try to get this data to show in a Toast message.

PushName

Navigator.pushNamed(
                            context,
                            '/navigator',
                            arguments: <String, String>{
                              'instalation': widget.instalation,
                              'message': DemoLocalizations.of(context)
                                      .text('cancel-message') +
                                  " " +
                                  widget.datameterValue.toString(),
                            },
                          );

Trying to retrieve data

        class Navigation extends StatefulWidget {
          final ConnectionPage args;
      Navigation({Key key, this.message, this.instalation, this.args}) : super(key: key);
    }

    class _NavigationState extends State<Navigation> {
  void initState() {
      super.initState();
        print(widget.args); //NULL

    final snackBar = SnackBar(
      duration: Duration(seconds: 5),
      content: Text(widget.args.messsage+ '.', textAlign: TextAlign.center),
      backgroundColor: Colors.red[700],
    );
    key.currentState.showSnackBar(snackBar);
    }
}

The problem: Return null.

So: What is the right way to get data using pushName? In the documentation show how can we get data inside Scaffold but i need to get data in the initState.

UPDATE

Routes

routes: {
    '/login': (context) => LoginPage(),
    '/navigator': (context) => Navigation(),
    '/home': (context) => HomePageScreen(),
    '/connect': (context) => ConnectionPage(),
  },  

UPDATE 2

I try something like this

 Navigator.pushNamed(
      context,
      '/navigator',
      arguments: Navigation(
          instalation: widget.instalation,
          message: DemoLocalizations.of(context)
         .text('cancel-message') +
         " " +
         widget.datameterValue.toString(),
     ),
);

解决方案

To do this in initState You need WidgetsBinding.instance.addPostFrameCallback and ModalRoute.of(context).settings.arguments
Demo pass arguments: {'instalation': "123", "message": "456"}
You can see full code and working demo picture below

code snippet use push

    Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => ExtractArgumentsScreen(),
                    // Pass the arguments as part of the RouteSettings. The
                    // ExtractArgumentScreen reads the arguments from these
                    // settings.
                    settings: RouteSettings(
                      arguments: {'instalation': "123", "message": "456"},
                    ),
                  ),
                );


@override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      final routeArgs1 =
          ModalRoute.of(context).settings.arguments as Map<String, String>;
      final instalation = routeArgs1['instalation'];
      final message = routeArgs1['message'];
      print('instalation ${instalation}');
      print('message ${message}');

      key.currentState
          .showSnackBar(SnackBar(content: Text(message)));
    });
  }

code snippet use Navigator.pushNamed

return MaterialApp(
      // Provide a function to handle named routes. Use this function to
      // identify the named route being pushed, and create the correct
      // Screen.
      routes: {
        '/extractArguments': (context) => ExtractArgumentsScreen(),
      },

...

Navigator.pushNamed(
                  context,
                  ExtractArgumentsScreen.routeName,
                  arguments: {'instalation': "123", "message": "456"},
                );

working demo

full code

    import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // Provide a function to handle named routes. Use this function to
      // identify the named route being pushed, and create the correct
      // Screen.
      routes: {
        '/extractArguments': (context) => ExtractArgumentsScreen(),
      },
      onGenerateRoute: (settings) {
        // If you push the PassArguments route
        if (settings.name == PassArgumentsScreen.routeName) {
          // Cast the arguments to the correct type: ScreenArguments.
          final ScreenArguments args = settings.arguments;

          // Then, extract the required data from the arguments and
          // pass the data to the correct screen.
          return MaterialPageRoute(
            builder: (context) {
              return PassArgumentsScreen(
                title: args.title,
                message: args.message,
              );
            },
          );
        }
      },
      title: 'Navigation with Arguments',
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home Screen'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            // A button that navigates to a named route that. The named route
            // extracts the arguments by itself.
            RaisedButton(
              child: Text("Navigate to screen that extracts arguments"),
              onPressed: () {
                // When the user taps the button, navigate to the specific route
                // and provide the arguments as part of the RouteSettings.
                 Navigator.pushNamed(
                  context,
                  ExtractArgumentsScreen.routeName,
                  arguments: {'instalation': "123", "message": "456"},
                );
                /*Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => ExtractArgumentsScreen(),
                    // Pass the arguments as part of the RouteSettings. The
                    // ExtractArgumentScreen reads the arguments from these
                    // settings.
                    settings: RouteSettings(
                      arguments: {'instalation': "123", "message": "456"},
                    ),
                  ),
                );*/

              },
            ),
            // A button that navigates to a named route. For this route, extract
            // the arguments in the onGenerateRoute function and pass them
            // to the screen.
            RaisedButton(
              child: Text("Navigate to a named that accepts arguments"),
              onPressed: () {
                // When the user taps the button, navigate to a named route
                // and provide the arguments as an optional parameter.
                Navigator.pushNamed(
                  context,
                  PassArgumentsScreen.routeName,
                  arguments: ScreenArguments(
                    'Accept Arguments Screen',
                    'This message is extracted in the onGenerateRoute function.',
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}

// A Widget that extracts the necessary arguments from the ModalRoute.
class ExtractArgumentsScreen extends StatefulWidget {
  static const routeName = '/extractArguments';

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

class _ExtractArgumentsScreenState extends State<ExtractArgumentsScreen> {
  final GlobalKey<ScaffoldState> key = new GlobalKey<ScaffoldState>();

  final snackBar = SnackBar(
    duration: Duration(seconds: 5),
    content: Text("message" + '.', textAlign: TextAlign.center),
    backgroundColor: Colors.red[700],
  );

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      final routeArgs1 =
          ModalRoute.of(context).settings.arguments as Map<String, String>;
      final instalation = routeArgs1['instalation'];
      final message = routeArgs1['message'];
      print('instalation ${instalation}');
      print('message ${message}');

      key.currentState
          .showSnackBar(SnackBar(content: Text(message)));
    });
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    // Extract the arguments from the current ModalRoute settings and cast
    // them as ScreenArguments.
    final routeArgs =
        ModalRoute.of(context).settings.arguments as Map<String, String>;
    final instalation = routeArgs['instalation'];
    final message = routeArgs['message'];

    return Scaffold(
      key: key,
      appBar: AppBar(
        title: Text(' ${routeArgs['code']} '),
      ),
      body: Column(
        children: <Widget>[
          Center(
            child: Text('instalation ${instalation}'),
          ),
          RaisedButton(
            onPressed: () {
              key.currentState.showSnackBar(snackBar);
            },
          ),
        ],
      ),
    );
  }
}

// A Widget that accepts the necessary arguments via the constructor.
class PassArgumentsScreen extends StatelessWidget {
  static const routeName = '/passArguments';

  final String title;
  final String message;

  // This Widget accepts the arguments as constructor parameters. It does not
  // extract the arguments from the ModalRoute.
  //
  // The arguments are extracted by the onGenerateRoute function provided to the
  // MaterialApp widget.
  const PassArgumentsScreen({
    Key key,
    @required this.title,
    @required this.message,
  }) : super(key: key);

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

// You can pass any object to the arguments parameter. In this example,
// create a class that contains both a customizable title and message.
class ScreenArguments {
  final String title;
  final String message;

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

这篇关于Flutter-使用pushName获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆