Flutter中启动带有返回结果的Activity等价于使用哪个函数?

2026-06-10 06:171阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计378个文字,预计阅读时间需要2分钟。

Flutter中启动带有返回结果的Activity等价于使用哪个函数?

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


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

Flutter中启动带有返回结果的Activity等价于使用哪个函数?

获取返回的数据

  • 通过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<String, dynamic> 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"});
},
),
),
);
}
}


本文共计378个文字,预计阅读时间需要2分钟。

Flutter中启动带有返回结果的Activity等价于使用哪个函数?

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


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

Flutter中启动带有返回结果的Activity等价于使用哪个函数?

获取返回的数据

  • 通过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<String, dynamic> 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"});
},
),
),
);
}
}