Widget build(BuildContext context) {
return StreamBuilder<User>(
stream: widget.auth.authStateChanges(),
builder: (context, snapshot) {
// showing Error in this part(The body might complete normally, causing
// 'null' to be returned, but the return type, 'Widget',
// is a potentially non-nullable type. Also showing
// type '_AsBroadcastStream<User?>' is not a subtype of type 'Stream<User>?')
if (snapshot.connectionState == ConnectionState.active) {
User? user = snapshot.Data;
if (_user == null) {
return LoginScreen(
onSignIn: _updateUser,
auth: widget.auth,
);
}
return Home(
onSignOut: () => _updateUser(null!),
auth: widget.auth,
);
}
},
);
}
在本部分中显示错误:
主体可能正常完成,导致返回'null‘,但是返回类型'Widget’是一个潜在的不可空类型.
。
发布于 2022-04-19 13:54:38
问题是在所有情况下,构建器都需要返回一个小部件。当if (snapshot.connectionState == ConnectionState.active)
不是真的时,它就不会这么做。例如,你可以这样做
Widget build(BuildContext context) {
return StreamBuilder<User>(
stream: widget.auth.authStateChanges(),
builder: (context, snapshot) {
// showing Error in this part(The body might complete normally, causing
// 'null' to be returned, but the return type, 'Widget',
// is a potentially non-nullable type. Also showing
// type '_AsBroadcastStream<User?>' is not a subtype of type 'Stream<User>?')
if (snapshot.connectionState == ConnectionState.active) {
User? user = snapshot.Data;
if (_user == null) {
return LoginScreen(
onSignIn: _updateUser,
auth: widget.auth,
);
}
return Home(
onSignOut: () => _updateUser(null!),
auth: widget.auth,
);
}
return SizedBox(); //you need to return some widget here. this is an example
},
);
}
https://stackoverflow.com/questions/71925864
复制相似问题