【发布时间】:2021-04-16 17:45:36
【问题描述】:
我有一个应用程序,我需要在实时数据库中保存数据,我有用于身份验证、存储和实时数据库的设置代码,身份验证和存储工作正常,但不是后者, 我尝试了不同的方法来使实时数据库工作,但不幸的是没有成功,任何帮助将不胜感激,谢谢。
- 这就是我初始化 firebase 的方式
FirebaseApp app;
void main() async {
WidgetsFlutterBinding.ensureInitialized();
app = await Firebase.initializeApp(
name: 'SecondaryApp',
options: const FirebaseOptions(
appId: '1:729669525962:android:b878xxxxxxxxxxxxx',
apiKey: 'AIzaSyD0w6UnBrWxxxxxxxxxxxxxxxxxxxxxx',
databaseURL: 'https://xxxxxxxxxxxx.appspot.com',
messagingSenderId: 'xxxxxxxxxxxxxxxx',
projectId: 'groceryfxxxxxxxxf6'
)
);
runApp(MaterialApp(
home: UserRegistrationScreen(),
debugShowCheckedModeBanner: false,
));
}
- 第 2 部分
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);
}
【问题讨论】:
-
代码乍一看还不错,虽然理想情况下我们应该看到
BuyerModel来确定。或者您可以简化代码。如果itemRef.child(firebaseAuth.currentUser.uid).set(true)也不起作用,您就知道模型不是问题所在。如果是这种情况,您可能需要检查 logcat 输出中set()中的权限错误或附加完成处理程序,如下所示:gist.github.com/puf/4a94a01e3c2510298ee46d0a7f90ab75 -
您使用
flutter和kotlin进行了标记。这是没有意义的,因为您可能只使用其中之一。因此,请更新您的标签,使其仅包含与我们在此处查看的问题相关的内容。 -
对不起,我把标签'kotlin',因为当我创建一个flutter项目时,它问我除了dart还用什么,我用kotlin,谢谢
-
这就是问题所在,例如,当我曾经在某处得到空异常时,它会显示,现在它不会在 logcat 中显示任何错误,这就是为什么我一直在尝试..但什么也没有
-
在 Flutter 中,我认为 Firebase SDK 不知道如何编写像
BuyerModel这样的自定义类。我通常以fromSnapshot和toMap方法来来回映射。但我希望 SDK 在您传递无法序列化的对象时抛出错误。
标签: firebase flutter dart firebase-realtime-database