在 Flutter 中将数据保存到 Firebase 实时数据库
Posted
技术标签:
【中文标题】在 Flutter 中将数据保存到 Firebase 实时数据库【英文标题】:Save Data to Firebase Realtime Database in Flutter 【发布时间】:2020-10-10 16:41:12 【问题描述】:在我的代码中,用户通过电话身份验证登录。登录后,我想获取用户的用户 ID,并将其连同其个人详细信息一起添加到 Firebase 数据库中。
但是当我这样做时,数据库中的路径是直接的,而不是应该通过第一个 UserId 然后在个人详细信息下。 我还提供了带有数据库输出的图像。
代码:
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/cupertino.dart';
import 'package:udharibook/Screens/SignInPage.dart';
import 'package:udharibook/Screens/dashboard.dart';
class AuthService
String UserId ='';
final DBRef = FirebaseDatabase.instance.reference().child('Users');
final FirebaseAuth _auth = FirebaseAuth.instance;
handleAuth()
return StreamBuilder(
stream: FirebaseAuth.instance.onAuthStateChanged,
builder: (BuildContext, snapshot)
if(snapshot.hasData)
getCurrentUser();
writeData();
return DashboardPage();
else
return SignIn();
,
);
getCurrentUser() async
final FirebaseUser user = await _auth.currentUser();
final uid = user.uid;
// Similarly we can get email as well
//final uemail = user.email;
UserId = uid;
print('User ID: '+UserId);
//print(uemail);
void writeData()
DBRef.child(UserId).set(
'id':'ID1',
'Name':'Mehul Jain',
'Phone':'8856061841'
);
signOut()
FirebaseAuth.instance.signOut();
signIn(AuthCredential authCreds)
FirebaseAuth.instance.signInWithCredential(authCreds);
signInWithOTP(smsCode,verId)
AuthCredential authCreds = PhoneAuthProvider.getCredential(
verificationId: verId,
smsCode: smsCode
);
signIn(authCreds);
数据库图片
【问题讨论】:
【参考方案1】:getCurrentUser()
方法是异步方法,因此在调用 writeData()
方法之前不确定您的 userId 是否具有值。
所以您可以在完成writeData()
方法后调用writeData()
,即:
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/cupertino.dart';
import 'package:udharibook/Screens/SignInPage.dart';
import 'package:udharibook/Screens/dashboard.dart';
class AuthService
String UserId ='';
final DBRef = FirebaseDatabase.instance.reference().child('Users');
final FirebaseAuth _auth = FirebaseAuth.instance;
handleAuth()
return StreamBuilder(
stream: FirebaseAuth.instance.onAuthStateChanged,
builder: (BuildContext, snapshot)
if(snapshot.hasData)
getCurrentUser();
//call writeData() from inside the above method
return DashboardPage();
else
return SignIn();
,
);
getCurrentUser() async
final FirebaseUser user = await _auth.currentUser();
final uid = user.uid;
// Similarly we can get email as well
//final uemail = user.email;
UserId = uid;
print('User ID: '+UserId);
//print(uemail);
//Here you add the method calling
writeData();
void writeData()
DBRef.child(UserId).set(
'id':'ID1',
'Name':'Mehul Jain',
'Phone':'8856061841'
);
signOut()
FirebaseAuth.instance.signOut();
signIn(AuthCredential authCreds)
FirebaseAuth.instance.signInWithCredential(authCreds);
signInWithOTP(smsCode,verId)
AuthCredential authCreds = PhoneAuthProvider.getCredential(
verificationId: verId,
smsCode: smsCode
);
signIn(authCreds);
【讨论】:
【参考方案2】:尝试以下方法:
void writeData() async
final FirebaseUser user = await _auth.currentUser();
final uid = user.uid;
DBRef.child(uid).set(
'id':'ID1',
'Name':'Mehul Jain',
'Phone':'8856061841'
);
删除getCurrentUser()
方法并在writeData()
中检索uid
【讨论】:
以上是关于在 Flutter 中将数据保存到 Firebase 实时数据库的主要内容,如果未能解决你的问题,请参考以下文章
Flutter-如何在flutter的整个应用程序生命周期中将数据保存在内存中?
如何在flutter中将用户数据保存在云Firestore中