我是扑扑的新手,我在通过共享首选项将Firestore数据保存到本地存储中时遇到错误,我尽力解决了此错误,但未找到任何解决方案。请提前致谢,我在geeting错误eror的代号名称的elsse部分为'DocumentSnapshot'类型的操作符'[]'未定义
GoogleSignInAccount googleUser = await googleSignIn.signIn();
GoogleSignInAuthentication googleAuth = await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
FirebaseUser firebaseUser = (await firebaseAuth.signInWithCredential(credential)).user;
if (firebaseUser != null) {
// Check is already sign up
final QuerySnapshot result =
await Firestore.instance.collection('users').where('id', isEqualTo:
firebaseUser.uid).get();
final List<DocumentSnapshot> documents = result.docs;
if (documents.length == 0) {
// Update data to server if new user
Firestore.instance.collection('users').document(firebaseUser.uid).setData({
'name': firebaseUser.displayName,
'photoUrl': firebaseUser.photoURL,
'id': firebaseUser.uid,
'createdAt': DateTime.now().millisecondsSinceEpoch.toString(),
'chattingWith': null
});
// Write data to local
currentUser = firebaseUser;
await prefs.setString('id', currentUser.uid);
await prefs.setString('nickname', currentUser.displayName);
await prefs.setString('photoUrl', currentUser.photoUrl);
} else {
// Write data to local
await prefs.setString('id', documents[0]['id']);
await prefs.setString('nickname', documents[0]['nickname']);
await prefs.setString('photoUrl', documents[0]['photoUrl']);
await prefs.setString('aboutMe', documents[0]['aboutMe']);
}
答案 0 :(得分:1)
您需要遍历文档以保存每个文档的数据,并且需要使用document.data:
GoogleSignInAccount googleUser = await googleSignIn.signIn();
GoogleSignInAuthentication googleAuth = await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
FirebaseUser firebaseUser = (await firebaseAuth.signInWithCredential(credential)).user;
if (firebaseUser != null) {
// Check is already sign up
final QuerySnapshot result =
await Firestore.instance.collection('users').where('id', isEqualTo:
firebaseUser.uid).get();
final List<DocumentSnapshot> documents = result.docs;
if (documents.length == 0) {
// Update data to server if new user
Firestore.instance.collection('users').document(firebaseUser.uid).setData({
'name': firebaseUser.displayName,
'photoUrl': firebaseUser.photoURL,
'id': firebaseUser.uid,
'createdAt': DateTime.now().millisecondsSinceEpoch.toString(),
'chattingWith': null
});
// Write data to local
currentUser = firebaseUser;
await prefs.setString('id', currentUser.uid);
await prefs.setString('nickname', currentUser.displayName);
await prefs.setString('photoUrl', currentUser.photoUrl);
} else {
// Write data to local
documents.forEach((DocumentSnapshot document){
Map<String,dynamic> documentData = document.data();
await prefs.setString('id', documentData['id']);
await prefs.setString('nickname', documentData['nickname']);
await prefs.setString('photoUrl', documentData['photoUrl']);
await prefs.setString('aboutMe', documentData['aboutMe']);
});
}