【发布时间】:2021-06-05 11:23:45
【问题描述】:
我对 Flutter 和 Firebase 还很陌生,我一直在关注 Reed Barger 的“使用 Flutter 和 Firebase 构建社交网络”https://www.udemy.com/course/build-a-social-network-with-flutter-and-firebase 的指南。我已成功通过 Google 完成身份验证,但在 Firestore 中创建用户时遇到问题。方法是
createUserInFirestore() async {
//1) check if user exists in user's collection in database (according to their id)
final GoogleSignInAccount user = googleSignIn.currentUser;
final DocumentSnapshot docu = await usersRef.doc(user.id).get();
//2) If user doesn't exist, take them to create account page
if (!docu.exists) {
final username = await Navigator.push(
context, MaterialPageRoute(builder: (context) => CreateAccount()));
//3) get user name from create account and use it to create new user's document in user's collection
usersRef.doc(user.id).set({
"id": user.id,
"username": username,
"photoUrl": user.photoUrl,
"email": user.email,
"displayName": user.displayName,
"bio": "",
"timestamp": timestamp
});
}
}
问题在于它不会将新用户带到 CreateAccount 页面,并且 Firestore 数据库也没有更新。相反,主页已加载,我收到错误
E/flutter (17311): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: NoSuchMethodError: The method 'doc' was called on null.
E/flutter (17311): Receiver: null
E/flutter (17311): Tried calling: doc("1148131790923706651234")
此外,用户不会添加到 Firebase 控制台中的 Users 下的 Authentication 中。目前,我只在 Android 上工作。
我哪里出错了?
【问题讨论】:
标签: flutter google-cloud-firestore