我已經實作了以下代碼。問題是我需要知道是否創建了用戶帳戶才能將用戶轉發到主螢屏。但由于我回傳的是 Fluttertoast(向用戶顯示錯誤訊息),因此我無法回傳 null。所以我想檢查回傳值是否是Fluttertoast。我怎樣才能做到這一點?還是有更好的方法來實作這一點?
Future registerWithEmailAndPassword(
{required String email,
required String password,
required String userName,
required String gender}) async {
try {
UserCredential result = await _auth.createUserWithEmailAndPassword(
email: email, password: password);
User? user = result.user;
// create a new document for the user with the uid
await DatabaseService(uid: user!.uid)
.updateUserData(gender: gender, userMail: email, userName: userName);
return user;
} catch (error) {
return Fluttertoast.showToast(
msg: error.toString(),
gravity: ToastGravity.TOP,
backgroundColor: Colors.black,
textColor: Colors.white);
}
}
基本上,然后我想實作一個 if 陳述句來檢查是否創建了用戶帳戶(然后回傳 fluttertoast)。就像是:
if (result == FToast()) {
print("user not created");
}
uj5u.com熱心網友回復:
嘗試回傳null而不是Fluttertoast.
將您的代碼更改為
Future registerWithEmailAndPassword(
{required String email,
required String password,
required String userName,
required String gender}) async {
try {
UserCredential result = await _auth.createUserWithEmailAndPassword(
email: email, password: password);
User? user = result.user;
// create a new document for the user with the uid
await DatabaseService(uid: user!.uid)
.updateUserData(gender: gender, userMail: email, userName: userName);
return user;
} catch (error) {
Fluttertoast.showToast(
msg: error.toString(),
gravity: ToastGravity.TOP,
backgroundColor: Colors.black,
textColor: Colors.white);
return null;
}
在此處顯示 toast 并回傳 null。
因此,您可以在 toast 中看到錯誤,然后您可以檢查用戶值是否為 null 創建的用戶。
if (result == null) {
print("user not created");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/512890.html
標籤:Google Cloud Collective 扑火力基地镖
