可能已经有人问过,但我找不到,但是如何将参数传递给命名路由?
这就是我建立路线的方式
Widget build(BuildContext context) {
return new Navigator(
initialRoute: 'home/chooseroom',
onGenerateRoute: (RouteSettings settings) {
WidgetBuilder builder;
switch (settings.name) {
case 'home/chooseroom':
// navigates to 'signup/choose_credentials'.
builder = (BuildContext _) => new ChoosePage();
break;
case 'home/createpage':
builder = (BuildContext _) => new CreateRoomPage();
break;
case 'home/presentation':
builder = (BuildContext _) => new Presentation();
break;
default:
throw new Exception('Invalid route: ${settings.name}');
}
return new MaterialPageRoute(builder: builder, settings: settings);
},
);
Run Code Online (Sandbox Code Playgroud)
这就是你的称呼
Navigator.of(context).pushNamed('home/presentation')
但是,如果我的小部件是new Presentation(arg1, arg2, arg3)怎么办?
我想在我的 statefulwidget 中触发构建之前获取我的用户 ID。如果我这样做,那么构建将在我获得我的 id 之前呈现。如果我将其放入 setstate,我的构建将首先使用空字符串,然后使用我的 id 再次重新渲染它,但这会导致不必要的行为。
那么我该如何解决这个问题呢?
String _userid = '';
Future<Null> setUserid() async {
SharedPreferences pref = await SharedPreferences.getInstance();
_userid = pref.getString('FB_USER');
}
initState() {
super.initState();
setUserid();
}
Run Code Online (Sandbox Code Playgroud)
建造
// Widget build
new Flexible(
child: new StreamBuilder<QuerySnapshot>(
stream: Firestore.instance
.collection('users')
.document(_userid)
.collection('rooms')
.snapshots(),
builder:
(BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return new Text('Loading...');
return new ListView(
children: snapshot.data.documents
.map(
(DocumentSnapshot document) => new Text('lol'),
// )
//new OverviewPresentation(presentation: document),
)
.toList(),
);
},
),
),
Run Code Online (Sandbox Code Playgroud) flutter ×2