startActivityForResult 在Flutter中等价于什么

网友投稿 704 2022-10-24 13:45:20

startActivityForResult 在Flutter中等价于什么

Flutter中所有路由的Navigator类可用于从已经push到栈的路由中获取结果。 这可以通过等待push返回的Future来完成,也可以从then中获取返回的数据。

获取返回的数据

通过await push返回的Future完成,await必须在异步函数中完成:

() async{ Map maps = await Navigator.push( context, MaterialPageRoute( builder: (context) => ProductPage({"name":"Tome"}), ), ); print(maps["name"]); },

从Future的then中获取

Navigator.push( context, MaterialPageRoute( builder: (context) => ProductPage({"name": "Tome"}), ), ).then((value) => {print(value["email"])});

向前返回数据

Navigator.pop(context, {"name": "Tom", "email": "tom@a3.com"});

跳转界面传参

Navigator.push( context, MaterialPageRoute( builder: (context) => ProductPage({"name": "Tome"}), ), )

完整例子

import 'package:flutter/material.dart';void main() => runApp(new MaterialApp( title: '导航演示1', home: new MyAppHome(), ));class MyAppHome extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('导航页面'), ), body: new Center( child: RaisedButton( child: Text("查看商品"), onPressed: () { Navigator.push( context, MaterialPageRoute( builder: (context) => ProductPage({"name": "Tome"}), ), ).then((value) => {print(value["email"])}); },// onPressed: () async{// Map maps = await Navigator.push(// context,// MaterialPageRoute(// builder: (context) => ProductPage({"name":"Tome"}),// ),// );// print(maps["name"]);// }, ), ), ); }}class ProductPage extends StatelessWidget { // 接受参数 final Map maps; // 接受参数 ProductPage(this.maps); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("商品详情"), ), body: Center( child: RaisedButton( child: Text("返回"), onPressed: () { print(maps["name"]); Navigator.pop(context, {"name": "Tom", "email": "tom@a3.com"}); }, ), ), ); }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:BootDo-JPA- 后台管理系统开发框架
下一篇:一款仿SpringMVC轻便的mvc开发框架
相关文章