我试图通过使用以下代码将管理员用户导航到特殊页面,但它给出了NoSuchMethodError
这是代码
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
final user = Provider.of<User>(context);
return StreamBuilder<DocumentSnapshot>(
stream: Firestore.instance.collection('users').document(user.uid).snapshots(),
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot){
if(snapshot.hasError){
return Text('Error: ${snapshot.error}');
}
switch(snapshot.connectionState){
case ConnectionState.waiting: return Loading();
default:
return checkRole(snapshot.data);
}
},
);
}
Widget checkRole(DocumentSnapshot snapshot){
if(snapshot.data['category']=='admin'){
return AproveNotice();
}else{
return HomePage();
}
}
}
这是我的错误The method '[]' was called on null. Receiver: null Tried calling: []("category")
最佳答案
您的snapshot.data
对象在这里是null
:if(snapshot.data['category']=='admin'){
这是因为如果对象不存在,快照数据将为null。 Google建议先检查它是否为exists
:
https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/DocumentSnapshot
关于firebase - Firebase中基于角色的身份验证,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62550128/