[英]Cloud Firestore Database is not displaying users after authentication?
我正在嘗試使用createUserWithEmailAndPassword
進行身份驗證。用戶 iis 正在創建,因為我可以使用 email 和使用 createUserWithEmailAndPassword 創建的密碼登錄。但是我的 firestore 數據庫沒有顯示我使用 createUserWithEmailAndPassword() 創建的文檔。
這是我的代碼:
onPressed: () async {
try {
final newuser = await FirebaseAuth.instance
.createUserWithEmailAndPassword(
email: email ?? 'error',
password: password ?? 'error',
);
if (newuser != null) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => home()),
);
}
} catch (e) {
print(e);
}
正如@ashish 已經提到的,您需要將所有額外參數作為文檔存儲在 firestore 中。 更好的實現方法是使用不同的 function 注冊用戶並創建與該注冊用戶相關的用戶文檔。
下面是我創建的一個例子來幫忙!
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
void main() {
String email = "test@test.com";
String password = "123456";
String firstName = "test";
String lastName = "test";
String phoneNumber = "09876543456";
/// This is the function to create new user
Future<void> createUser(
email,
password,
firstName,
lastName,
phoneNumber,
/*... and any other params you wish to collect */
) async {
// firebase auth
final FirebaseAuth auth = FirebaseAuth.instance;
// firestore db
FirebaseFirestore db = FirebaseFirestore.instance;
// try-catch creation process to accurate exception
try {
final credential = await auth.createUserWithEmailAndPassword(
email: email,
password: password,
);
// get user id
String userID = credential.user!.uid;
// creating user profile on db
await db.collection("users").doc(userID).set({
"id": userID,
"firstName": firstName,
"lastName": lastName,
"phoneNumber": phoneNumber,
// ... other params
});
print("Signed up successfully!");
} on FirebaseAuthException catch (error) {
print("Something is wrong: $error");
}
}
/// this is the create button
TextButton(
onPressed: () async {
await createUser(
email,
password,
firstName,
lastName,
phoneNumber,
/*... and any other params you wish to collect */
);
},
child: const Text("Create test user"),
);
}
需要幫助請叫我:)。 再見!!!
要將數據存儲在數據庫中,您需要使用 Firestore 實例將數據存儲在 firebase Firestore 中。
onPressed: () async {
try {
final newuser = await FirebaseAuth.instance
.createUserWithEmailAndPassword(
email: email ?? 'error',
password: password ?? 'error',
);
await FirebaseFirestore.instance.collection('Users').doc(newuser!.uid).set({
email: newuser.email
});
if (newuser != null) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => home()),
);
}
} catch (e) {
print(e);
}
}
您需要將要存儲在 firestore 中的數據放在set()
function 中。
Firebase 身份驗證數據顯示在登錄 firebase 然后點擊身份驗證選項菜單 -> 用戶部分,您可以在該部分查看存儲 3 個用戶數據
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.