将数据保存到Flatter中的Firebase实时数据库
在我的代码中,用户是通过电话身份验证登录的。登录后,我想获取用户的用户id,并将其与个人详细信息一起添加到Firebase数据库中 但当我完成这项工作时,数据库中的路径是直接的,而不是通过第一个用户ID,然后在该用户ID下显示个人详细信息。 我还提供了数据库输出的图像 代码: 数据库映像请尝试以下操作:将数据保存到Flatter中的Firebase实时数据库,firebase,flutter,dart,firebase-realtime-database,firebase-authentication,Firebase,Flutter,Dart,Firebase Realtime Database,Firebase Authentication,在我的代码中,用户是通过电话身份验证登录的。登录后,我想获取用户的用户id,并将其与个人详细信息一起添加到Firebase数据库中 但当我完成这项工作时,数据库中的路径是直接的,而不是通过第一个用户ID,然后在该用户ID下显示个人详细信息。 我还提供了数据库输出的图像 代码: 数据库映像请尝试以下操作: void writeData() async{ final FirebaseUser user = await _auth.currentUser(); final uid
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尝试以下操作:
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中的uidgetCurrentUser方法是一个异步方法,因此在调用writeData方法之前,不确定您的用户ID是否具有值
因此,您可以在getCurrentUser方法完成后调用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);
}
}
getCurrentUser方法是一个异步方法,因此在调用writeData方法之前,不确定您的用户ID是否具有值
因此,您可以在getCurrentUser方法完成后调用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);
}
}