Firebase 实时数据库颤振中未保存数据
我有一个应用程序,我需要在实时数据库中保存数据,我有身份验证、存储和实时数据库的设置代码,身份验证和存储工作正常,但不是后者, 我尝试了不同的方法使实时数据库工作,但不幸的是没有成功,任何帮助将不胜感激。谢谢Firebase 实时数据库颤振中未保存数据,firebase,flutter,dart,firebase-realtime-database,Firebase,Flutter,Dart,Firebase Realtime Database,我有一个应用程序,我需要在实时数据库中保存数据,我有身份验证、存储和实时数据库的设置代码,身份验证和存储工作正常,但不是后者, 我尝试了不同的方法使实时数据库工作,但不幸的是没有成功,任何帮助将不胜感激。谢谢 这就是我初始化firebase的方式 第二部分 这是买方模式 在Flatter中,我认为Firebase SDK不知道如何编写自定义类,如BuyerModel。我通常使用fromSnapshot和toMap方法来来回映射 此类类别的一个示例: class Question {
- 这就是我初始化firebase的方式
- 第二部分
- 这是买方模式
在Flatter中,我认为Firebase SDK不知道如何编写自定义类,如
BuyerModel
。我通常使用fromSnapshot
和toMap
方法来来回映射
此类类别的一个示例:
class Question {
HUser owner;
String title;
List<String> answers;
int timeInSeconds;
String key;
Question(String this.title, List<String> this.answers, this.timeInSeconds, { this.owner }) {
}
Map<String, dynamic> toMap() {
return <String,dynamic>{
"owner": owner?.uid,
"title": title,
"answers": answers,
"timeInSeconds": timeInSeconds
};
}
Question.fromSnapshot(DataSnapshot snapshot): assert(snapshot != null),
title = snapshot.value["title"],
timeInSeconds = snapshot.value["timeInSeconds"],
owner = snapshot.value["owner"] != null ? HUser(snapshot.value["owner"]) : null,
key = snapshot.key;
}
课堂提问{
户主;
字符串标题;
列出答案;
int-timeinsonds;
字符串键;
问题(字符串this.title,列出this.answers,this.timeinsectonds,{this.owner}){
}
映射toMap(){
返回{
“所有者”:所有者?.uid,
“头衔”:头衔,
“答案”:答案,
“timeInSeconds”:timeInSeconds
};
}
问题.fromSnapshot(DataSnapshot快照):断言(快照!=null),
title=快照。值[“title”],
timeInSeconds=snapshot.value[“timeInSeconds”],
所有者=快照。值[“所有者”]!=null?HUser(快照。值[“所有者”]):null,
key=snapshot.key;
}
现在我明白了,我仍然需要从快照中读取
答案。这可能解释了我的应用程序中的错误。代码乍一看很好,但理想情况下,我们可以肯定地看到BuyerModel
。或者您可以简化代码。如果itemRef.child(firebaseAuth.currentUser.uid).set(true)
也不起作用,您就知道模型不是问题所在。如果是这种情况,您可能希望在set()
中检查logcat输出是否存在权限错误,或者附加一个完成处理程序,如下所示:您使用flatter
和kotlin
标记。这毫无意义,因为您可能只使用其中一种。因此,请更新您的标签,使其仅包含与我们正在查看的问题相关的内容。很抱歉,我实际添加了标签“kotlin”,因为当我创建一个颤振项目时,它会问我使用什么,除了dart,我使用kotlin,我会保存它。谢谢,这就是问题所在,例如,当我在某处获得一个空异常时,它显示,现在它在logcat中没有显示任何错误,这就是我一直在尝试的原因。但是在Flutter中没有任何错误。我认为Firebase SDK不知道如何编写自定义类,如BuyerModel
。我通常使用fromSnapshot
和toMap
方法来来回映射。但是当你传递一个无法序列化的对象时,我希望SDK会抛出一个错误。非常感谢你的努力和时间,最后一件事,现在请将我的模型传递给set()方法,是toMap(),还是像我以前在代码中做的那样直接传递buyermodel?然后你应该传递toMap()
将值返回到set
方法。实际上,我是这样做的,我复制了粘贴了您的模型类,只需更改为我自己的值,但仍然没有任何效果“nothing is working”确实很难帮助。我只是以我的类为例,您肯定需要创建自己的类来进行类似的转换。当您在完成这项工作后逐步完成代码时,它是否完成了您期望它完成的任务?哪一行是第一行没有的?所有变量都有哪些值?我有在java和kotlin中使用firebase的经验,这与在Flatter中使用firebase有点相似,但我已经添加了所有必要的组件使其工作,因为身份验证和存储工作正常,我有firebase_数据库插件,做了所有事情,这让我很困惑,我在这一点上被卡住了
class _UserRegistrationScreenState extends State<UserRegistrationScreen> {
DatabaseReference itemRef;
final firebaseAuth = FirebaseAuth.instance;
final storage = FirebaseStorage.instance;
@override
void initState() {
super.initState();
itemRef = FirebaseDatabase.instance.reference().child('Users');
}
BuyerModel model = new BuyerModel(
fullName,phone,country,state,city,address,
email,'online','Buyer', downloadUrl,DateTime.now().microsecondsSinceEpoch.toString(),
firebaseAuth.currentUser.uid);
itemRef.child(firebaseAuth.currentUser.uid).set(model);
class BuyerModel {
String fullName;
String phone;
String country;
String state;
String city;
String address;
String email;
String status;
String accountType;
String profileImage;
String timeStamp;
String userUid;
BuyerModel(this.fullName,this.phone,this.country,this.state,this.city,
this.address,this.email,this.status,this.accountType,this.profileImage,
this.timeStamp,this.userUid);
}
class Question {
HUser owner;
String title;
List<String> answers;
int timeInSeconds;
String key;
Question(String this.title, List<String> this.answers, this.timeInSeconds, { this.owner }) {
}
Map<String, dynamic> toMap() {
return <String,dynamic>{
"owner": owner?.uid,
"title": title,
"answers": answers,
"timeInSeconds": timeInSeconds
};
}
Question.fromSnapshot(DataSnapshot snapshot): assert(snapshot != null),
title = snapshot.value["title"],
timeInSeconds = snapshot.value["timeInSeconds"],
owner = snapshot.value["owner"] != null ? HUser(snapshot.value["owner"]) : null,
key = snapshot.key;
}