我正在嘗試查找登錄用戶是管理員還是普通用戶。我已經創建了一個全域函式(然后在 中呼叫它initState)通過將 bool 設定為 true 或 false 來檢查角色是否為 admin ,如下所示:
bool isAdmin;
_checkRole() async {
var firebaseUser = FirebaseAuth.instance.currentUser;
await FirebaseFirestore.instance
.collection("users")
.doc(firebaseUser.uid)
.get()
.then((value) {
if ((value.data()['role']) == 'admin') {
isAdmin = true;
} else {
isAdmin = false;
}
return isAdmin;
});
}
在抽屜里,我做了以下事情:
isAdmin
? buildListTile(
'Admin Panel', Icons.admin_panel_settings_sharp, () {
Navigator.of(context).pushNamed(AdminScreen.routeName);
})
: buildListTile('User dashboard', Icons.person, () {}),
但是當我打開抽屜時,我收到了Failed assertion: boolean expression must not be null
關于如何解決這個問題的任何想法?
謝謝。
uj5u.com熱心網友回復:
簡短回答:
當您構建 ListTile 時,isAdmin 未初始化,因為異步函式還沒有機會完成運行。
更長的答案:
您的build方法與其余代碼同步發生,這意味著它一行接一行地發生。你的_checkRole()方法是異步發生的,這意味著只要它繞過它就會繞過它。因此,當您嘗試isAdmin在您的initState方法中初始化時,它正在運行網路呼叫(就程式時間而言,這需要很長時間)并等待網路呼叫完成設定isAdmin。同時,您的構建正在運行并嘗試構建而不知道它應該等待isAdmin設定。
一個解決方案:(
注意,有很多方法可以解決這個問題,這只是一個)
使用 FutureBuilder 或 StreamBuilder 加載變數并將變數型別設定為 Future 或流的等效項,然后監聽狀態變化并構建您的 UI因此。
這是一個基本的例子。小心復制/粘貼。我沒有運行代碼。這只是一般的想法。
Future<bool> isAdmin;
FutureBuilder<String>(
future: Globals.isAdmin,
builder: (BuildContext context, AsyncSnapshot<Bool> snapshot) {
if (snapshot.hasData) { //
var isAdmin = snapshot.data;
// use the value for isAdmin
if (isAdmin == true) {
return Container();
} else {
return Container();
}
} else if (snapshot.hasError) {
//handle your error
return Container();
} else {
// handle your loading
return CircularProgressIndicator();
}
},
),
uj5u.com熱心網友回復:
嘗試以下一種方式更改您的 _checkRole() 方法:
Future<bool> _checkRole() async {
var firebaseUser = FirebaseAuth.instance.currentUser;
return await FirebaseFirestore.instance
.collection("users")
.doc(firebaseUser.uid)
.get()
// we create the new Future with bool value, depending on
// the Firebase response and throw it away as a result of
// the _checkRole method
.then((value) => Future.value(value.data()['role']) == 'admin'));
}
然后在您的組件中使用FutureBuilder。所以你的布局應該是這樣的:
child: FutureBuilder<bool>(
future: _checkRole,
builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
if (snapshot.hasData) { // check whether we have any data in our Future object
final isAdmin = snapshot.data; // bool type
return isAdmin
? buildListTile(
'Admin Panel', Icons.admin_panel_settings_sharp, () {
Navigator.of(context).pushNamed(AdminScreen.routeName);
})
: buildListTile('User dashboard', Icons.person, () {}),
}
// if snapshot doesn't have data return a widget with an error message
return Center(
child: Text('Error!'),
);
},
),
uj5u.com熱心網友回復:
為 isAdmin 變數設定默認值
bool isAdmin = false;
或者如果你有一個成員模型,我的意思是你創建了一個名為 Member 的類
class Member {
// create a method for deteriming if the the member is an admin or not
bool isAdmin() async {
var firebaseUser = FirebaseAuth.instance.currentUser;
await FirebaseFirestore.instance
.collection("users")
.doc(firebaseUser.uid)
.get()
.then((value) {
return ((value.data()['role']) == 'admin');
});
}
}
現在,您可以使用此方法來檢查成員是否為管理員
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/377564.html
標籤:火力基地 扑 镖 谷歌云firestore
