firebase 中基于角色的身份验证
role based authentication in firebase
我尝试使用以下代码将管理员用户导航到一个特殊页面,但它给出了 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")
改变这个:
case ConnectionState.waiting: return Loading();
default:
return checkRole(snapshot.data);
}
对此:
case ConnectionState.waiting: return Loading();
case ConnectionState.done: return checkRole(snapshot.data);
}
您的 snapshot.data
对象在 null
此处:
if(snapshot.data['category']=='admin'){
这是因为如果对象不存在,快照数据将为空。 Google 建议首先检查它 exists
:
https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/DocumentSnapshot
If the DocumentSnapshot points to a non-existing document, getData() and its corresponding methods will return null. You can always explicitly check for a document's existence by calling exists().
我尝试使用以下代码将管理员用户导航到一个特殊页面,但它给出了 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")
改变这个:
case ConnectionState.waiting: return Loading();
default:
return checkRole(snapshot.data);
}
对此:
case ConnectionState.waiting: return Loading();
case ConnectionState.done: return checkRole(snapshot.data);
}
您的 snapshot.data
对象在 null
此处:
if(snapshot.data['category']=='admin'){
这是因为如果对象不存在,快照数据将为空。 Google 建议首先检查它 exists
:
https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/DocumentSnapshot
If the DocumentSnapshot points to a non-existing document, getData() and its corresponding methods will return null. You can always explicitly check for a document's existence by calling exists().