[英]NoSuchMethodError was thrown building StreamBuilder<DocumentSnapshot>
Actually I am following a tutorial for role based authentication.实际上,我正在关注基于角色的身份验证教程。 Problem is that when I first login, I am navigated to the right screen as expected and snapshot does have data!
问题是当我第一次登录时,我按预期导航到正确的屏幕并且快照确实有数据!
User: {role: admin, last_login: Timestamp(seconds=1610430754, nanoseconds=74000000), name: bilal, created_at: Timestamp(seconds=1610429038, nanoseconds=204000000), build_number: 1, email: bilalsaeed781833@gmail.com}
But when I create another user onto the moved screen and the same Stream builder again check for role based authentication, snapshot doesn't have data.但是当我在移动的屏幕上创建另一个用户并且同一个 Stream 构建器再次检查基于角色的身份验证时,快照没有数据。
User: null UserDoc: Instance of 'DocumentSnapshot'
======== Exception caught by widgets library =======================================================
The following NoSuchMethodError was thrown building StreamBuilder<DocumentSnapshot>(dirty, state: _StreamBuilderBaseState<DocumentSnapshot, AsyncSnapshot<DocumentSnapshot>>#0f4cc):
The method '[]' was called on null.
Receiver: null
Tried calling: []("role")
I don't know why its giving that error!我不知道为什么会出现该错误!
Here is my code:这是我的代码:
return StreamBuilder<User>(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.active) {
return StreamBuilder<DocumentSnapshot>(
stream: FirebaseFirestore.instance
.collection("users")
.doc(snapshot.data.uid)
.snapshots(),
builder: (BuildContext context,
AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.connectionState == ConnectionState.active) {
final userDoc = snapshot.data;
final user = userDoc.data();
debugPrint("User: ${user.toString()} UserDoc: ${userDoc.toString()} ");
if (user['role'] == 'admin') {
return admin();
} else if (user['role'] == 'hod') {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => hod()),
);
} else if (user['role'] == 'duo') {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => duo()),
);
} else if (user['role'] == 'teacher') {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => teacher()),
);
} else if (user['role'] == 'cr') {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => cr()),
);
} else if (user['role'] == 'student') {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => student()),
);
} else {
return Material(
child: Center(
child: CircularProgressIndicator(),
),
);
}
}
return Material(
child: Center(
child: CircularProgressIndicator(),
),
);
},
);
}
return Material(
child: Center(
child: CircularProgressIndicator(),
),
);
});
Wrap everything inside your StreamBuilder in an if statement.在 if 语句中包装 StreamBuilder 中的所有内容。
if (snapshot.connectionState == ConnectionState.active) {
if(snapshot.hasData){
...
} else {
return CircularProgressIndicator()
}
}
This should prevent the builder from trying to build something when it has no data.这应该可以防止构建器在没有数据时尝试构建某些东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.