【发布时间】:2021-05-06 08:32:18
【问题描述】:
我正在学习 Flutter,作为练习,我正在与 Firebase 交互(我也是新手——我的第一次接触)。 在练习中,当我们注册一个新用户时,我们会为新注册的用户创建一个具有默认值的文档。
一个新用户正在注册,但集合和文档都没有创建。
我创建的 Cloud Firestore 是测试模式。下面附上规则。
Rules :
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if
request.time < timestamp.date(2021, 3, 4);
}
}
}
auth.dart
//Resgister using email and password
Future registerWithEmailPassword(
{@required String email, @required String password}) async {
try {
Firebase.UserCredential resut = await _auth
.createUserWithEmailAndPassword(email: email, password: password);
Firebase.User user = resut.user;
//create a new document for user with uid
await DatabaseService(uid: user.uid).updateUserData(
sugars: '0',
name: 'new crew member',
strength: 100,
);
return _userFromFirebase(user);
} catch (e) {
//print(e.toString());
return e.toString();
}
}
database.dart
import 'package:cloud_firestore/cloud_firestore.dart';
class DatabaseService { final String uid; DatabaseService({this.uid});
//colection reference
final CollectionReference brewCollection =
FirebaseFirestore.instance.collection('brews');
Future updateUserData({String sugars, String name, int strength}) async {
return await brewCollection
.doc(uid)
.set({'sugars': sugars, 'name': name, 'strength': strength}); } }
}
我加了一个断点,检查updateUserData中的数据,数据是正确的。
【问题讨论】:
-
在你
DatabaseService类中,brewCollection被注释掉了! -
@ASADHAMEED,当我将代码粘贴到堆栈溢出的编辑器中时,这就是问题所在。我检查了代码 final CollectionReference brewCollection = FirebaseFirestore.instance.collection('brews');没有评论,问题正在形成。
-
你想从updateUserData返回什么?
-
@RehmatSinghGill,目前我不希望返回任何东西。稍后我将尝试使用更新状态进行布尔运算。
-
你的代码看起来不错;
catch块中是否有任何异常?
标签: flutter google-cloud-firestore