参数为 null 而不是带有 ModalRoute.of() 的 id [英] Parameter is null instead of an id with ModalRoute.of()
本文介绍了参数为 null 而不是带有 ModalRoute.of() 的 id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 Flutter 和 Firebase 开发移动应用.
I am developing a mobile App with Flutter and Firebase.
我正在尝试使用 pushNamed() 并传递一个参数.(身份证)
I am trying to use pushNamed() and hand over a parameter. (an id)
我不知道如何解决我的问题.
I don't know how i could solve my problem.
这是我的代码:
@override
void didChangeDependencies() {
if (_isInit) {
print(ModalRoute.of(context).settings.arguments);
final productId = ModalRoute.of(context).settings.arguments;
if (productId != null) {
_editedAngebot = Provider.of<Angebote>(context).findByID(productId);
_initValues = {
'titel': _editedAngebot.titel,
'beschreibung': _editedAngebot.beschreibung,
'semester': _editedAngebot.semester.toString(),
'fach': _editedAngebot.fach,
'abteilung': _editedAngebot.abteilung,
};
}
}
_isInit = false;
super.didChangeDependencies();
}
还有另一个类,我在那里设置了参数.我的Angebot"对象只有一个默认构造函数.
And the other class, where I set the parameter. My "Angebot" object only has a default constructor.
trailing: isAllowed()
? IconButton(
icon: Icon(Icons.edit),
onPressed: () {
Navigator.of(context).maybePop();
Navigator.of(context)
.pushNamed('/editAngebot', arguments: id);
})
为什么我的 ID 为空?
Why is my ID null?
推荐答案
您的 Id 为空,因为您要先弹出一个页面,然后再推送新页面.
Your Id is null because you are popping a page first then pushing new page .
使用 pushReplacementNamed()
Use pushReplacementNamed()
这是一个代码示例
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: FirstPage(),
routes:{
Secondpage.routeName:(context)=>Secondpage(),
}
);
}
}
class FirstPage extends StatelessWidget {
final String id = '01';
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child:ElevatedButton(
child: Text('GoTo secondPage'),
onPressed: (){
Navigator.of(context).pushReplacementNamed(Secondpage.routeName,arguments: id);
},
))
);
}
}
class Secondpage extends StatelessWidget {
static const routeName = 'secondpage';
@override
Widget build(BuildContext context) {
final data = ModalRoute.of(context).settings.arguments as String;
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Center(child:Text('$data')),
ElevatedButton(
child: Text('GoTo FirstPage'),
onPressed: (){
Navigator.of(context).pop();
},
)
],
)
);
}
}
这篇关于参数为 null 而不是带有 ModalRoute.of() 的 id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文