如何将多个变量传递到新屏幕,然后将该数据传递回? 我可以成功地传递回用户名,但是现在我至少需要6个变量。
到目前为止,这就是我提出的。我似乎无法弄清楚。
Future<void> createUserInFirestore(FirebaseUser currentUser) async {
// 1) check if user exists in user collection in database (according to ID)
DocumentSnapshot doc = await usersRef.document(currentUser.uid).get();
if (!doc.exists) {
// 2) if user does not exist, take them to user create account.
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => new CreateAccount(username: username),
),
);
// 3) get username from create account, use it to make new user documents in users collection.
usersRef.document(currentUser.uid).setData({
"uid": currentUser.uid,
"username": username,
"user_profile_photo": currentUser.photoUrl == null? {} : currentUser.photoUrl,
"email": currentUser.email,
"account_created": timestamp,
});
}
这是我提交方法的回调;
_submit() {
final form = _formKey.currentState;
if (form.validate()) {
form.save();
SnackBar snackbar = SnackBar(
content: Text("Welcome $widget.username!"),
);
_scaffoldKey.currentState.showSnackBar(snackbar);
Timer(Duration(seconds: 2), () {
Navigator.pop(context, username);
});
}
我已经被困了4天,试图弄清楚这一点。
答案 0 :(得分:2)
编辑:好吧,您说我对一个变量做了,并要求至少6个变量。
Map<String, dynamic> yourMap = {
"uid": currentUser.uid,
"username": username,
"user_profile_photo": currentUser.photoUrl == null? {} : currentUser.photoUrl,
"email": currentUser.email,
"account_created": timestamp,
};
1-像这样更新Navigator.pop
Navigator.pop(context, yourMap}
2-像这样更新Navigator.push
Navigator.push(context, MaterialPageRoute(builder: (_) => WhichPageYouWantPage(yourMap)));
您的CreateAccount
代码很好,只需像这样更新Navigator.push
(U可以带回所有内容)String username = await Navigator.push(...);
答案 1 :(得分:1)
Map<String, dynamic> data = {
// your data in form of map
};
使用以下方法将数据传递到您的页面:
Navigator.push(context, MaterialPageRoute(builder: (_) => YourPage(data)));
然后像在页面中检索数据一样
class YourPage extends StatelessWidget {
final data;
YourPage(this.data);
// other code
}