我有一个 Product 类,它在 List plist 中 现在我需要调用 firebase database.set(plist) 这正在使用 Java 但是当我尝试使用 flutter dart 时它显示错误任何人都有解决这个问题的方法 从 StackOverflow,我了解使用 database.set('{"a":"apple"}) 但是当我处理 List 时我不能使用这个解决方案
更新错误信息
error called Invalid argument: Instance of 'Product'
我的代码
String table_name="order";
FirebaseAuth.instance.currentUser().then((u){
if(u!=null){
FirebaseDatabase database = FirebaseDatabase(app: app);
String push=database.reference().child(table_name).child(u.uid).push().key;
database.reference().child(table_name).child(u.uid).child(push).set( (productList)).then((r){
print("order set called");
}).catchError((onError){
print("order error called "+onError.toString());
});
}
});
}
最佳答案
我们不能直接在 Firebase 中设置对象。不幸的是,在 Flutter 中没有像 java json 这样简单的解决方案。 允许的数据类型有 String、boolean、int、double、Map、List。在 database.set() 中。
可以看看Flutter的官方文档https://pub.dev/documentation/firebase_database/latest/firebase_database/DatabaseReference/set.html
尝试像这样设置对象
Future<bool> saveUserData(UserModel userModel) async {
await _database
.reference()
.child("Users")
.child(userModel.username)
.set(<String, Object>{
"mobileNumber": userModel.mobileNumber,
"userName": userModel.userName,
"fullName": userModel.fullName,
}).then((onValue) {
return true;
}).catchError((onError) {
return false;
});
希望这段代码对您有所帮助。
关于firebase - Flutter firebase database.set(object) 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54106751/