dart - Flutter - Pass values between routes using pushReplacementNamed
问题描述
I'm trying to pass values between routes using Navigator.pushReplacementNamed()
, but I'm not getting it.
I can not figure out what the correct syntax and logic is in which data will exit pushReplacementNamed
and get the routes
key.
Can you help me?
Enter the code below to facilitate understanding:
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'])
],
),
);
}
}
After correction:
The purpose of trying to use pushReplacementNamed
was to remove the route history, so if the user presses the device's back button to get back to the earlier route, the route would no longer exist. Reference
The reason for this implementation is to be used on a login page, if the user has already been authenticated by google sign
he is redirected to the next page passing login parameters and could not return to the login page by device's back button.
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'])
],
),
);
}
}
解决方案
我认为您可能会错过了解结果的工作原理。结果返回到上一个推送最后一个路由的小部件,并await
用于Future
由上一次调用返回的结果push
。你只是想用参数开始一条路线吗?然后你可以这样做。
Navigator.of(context).pushReplacement(
new MaterialPageRoute(
settings: const RouteSettings(name: '/form'),
builder: (context) => new FormPage(
email: 'myemail@flutter.com',
header: {'auth': '1234'},
),
),
);
推荐阅读
- google-sheets - 为多个具有相同名称的 Excel 工作表从 Google Drive 获取一个稳定的链接到 PowerBi
- sql - SQLPlus 报告中的列显示时间超过数据库列大小
- html - 使用 flexbox 对齐一行中的 3 个项目
- java - 通过使用 spring boot 和 myBatis 执行 findById 来结束事务
- email - 从 mailgun 中的组织帐户中删除子帐户
- ruby-on-rails - 使用 devise、omniauth 和 devise-token-auth 时,没有路由匹配“omniauth/:provider”
- c++ - 如何获取 lambda *capture* 函数的“原始”ptr
- http - RTSP over HTTP 用于 ip cam 流的优势
- javascript - ER_PARSE_ERROR,错误:1064 - 找不到我的语法有什么问题 - 它适用于工作台
- javascript - 如何使用 jQuery 使用带有 PHP 的 td 输出的表不检查禁用的复选框