gpt4 book ai didi

dart - Flutter - 使用 pushReplacementNamed 在路由之间传递值

转载 作者:IT王子 更新时间:2023-10-29 07:08:04 26 4
gpt4 key购买 nike

我正在尝试使用 Navigator.pushReplacementNamed() 在路由之间传递值,但我没有得到它。

我无法弄清楚数据将退出 pushReplacementNamed 并获取 routes 键的正确语法和逻辑是什么。

你能帮帮我吗?

输入下面的代码,方便理解:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(),
routes: <String, WidgetBuilder> {
'/form': (BuildContext context) => new FormPage(email: ???, header: {'auth': ???}),
},
);
}
}

class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return new Scaffold(
floatingActionButton: new FloatingActionButton(
onPressed: () {
Navigator.pushReplacementNamed(context, '/form', {email: 'myemail@flutter.com', header: {'auth': '1234'}});
},
child: new Icon(Icons.navigate_next),
),
);
}
}

class FormPage extends StatefulWidget {
FormPage({Key key, this.email, this.header}) : super(key: key);
String email;
Map header;
@override
FormPageState createState() => new FormPageState();
}

class FormPageState extends State<FormPage> {
@override
Widget build(BuildContext context) {
return new Container(
child: new Column(
children: <Widget>[
new Text(widget.email),
new Text(widget.header['auth'])
],
),
);
}
}

修正后:

尝试使用 pushReplacementNamed 的目的是删除路线历史记录,因此如果用户按下设备的后退按钮返回到较早的路线,则该路线将不再存在。 Reference

这个实现的原因是在登录页面上使用,如果用户已经通过 google sign 的身份验证,他将被重定向到下一个传递登录参数的页面,并且无法返回到通过设备的后退按钮登录页面。

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(),
routes: <String, WidgetBuilder> {
'/form': (BuildContext context) => new FormPage(), //new
},
);
}
}

class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return new Scaffold(
floatingActionButton: new FloatingActionButton(
onPressed: () {
Navigator.of(context).pushReplacement( //new
new MaterialPageRoute( //new
settings: const RouteSettings(name: '/form'), //new
builder: (context) => new FormPage(email: 'myemail@flutter.com', header: {'auth': '1234'}) //new
) //new
); //new
},
child: new Icon(Icons.navigate_next),
),
);
}
}

class FormPage extends StatefulWidget {
FormPage({Key key, this.email, this.header}) : super(key: key);
String email;
Map header;
@override
FormPageState createState() => new FormPageState();
}

class FormPageState extends State<FormPage> {
@override
Widget build(BuildContext context) {
return new Container(
child: new Column(
children: <Widget>[
new Text(widget.email),
new Text(widget.header['auth'])
],
),
);
}
}

最佳答案

我认为您可能不了解 result 的工作原理。结果返回到推送最后一条路线的前一个小部件,并使用 await 作为 Future 的结果,该 Future 由之前调用 push。你只是想用参数开始一条路线吗?然后你可以这样做。

Navigator.of(context).pushReplacement(
new MaterialPageRoute(
settings: const RouteSettings(name: '/form'),
builder: (context) => new FormPage(
email: 'myemail@flutter.com',
header: {'auth': '1234'},
),
),
);

关于dart - Flutter - 使用 pushReplacementNamed 在路由之间传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50088715/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com