断言失败:布尔表达式不能为空(对于文档快照数据)
Failed assertion: boolean expression must not be null (for documentsnapshot data)
当我运行下面的代码时,我得到如下所示的错误消息:
======== Exception caught by widgets library ======================================================= The following assertion was thrown building StreamBuilder(dirty,
state: _StreamBuilderBaseState<DocumentSnapshot,
AsyncSnapshot>#cfcf8): Failed assertion: boolean
expression must not be null
我运行的代码如下图:
class DataState extends AppState {
final CollectionReference dataCollection =
FirebaseFirestore.instance.collection('data');
Stream<DocumentSnapshot> dataStream({String uid}) =>
dataCollection.doc(uid).snapshots();
class WaitingPage extends StatefulWidget {
@override
_WaitingPageState createState() => _WaitingPageState();
}
class _WaitingPageState extends State<WaitingPage> {
@override
Widget build(BuildContext context) {
final AuthState userProvider = Provider.of<AuthState>(context);
var dataState = Provider.of<DataState>(context, listen:false);
return (userProvider.userModel != null)
? StreamBuilder<DocumentSnapshot>(
stream: dataState.dataStream(uid: userProvider.userModel.userId),
builder: (context, snapshot) {
if (snapshot.hasData && snapshot.data.data() != null) {
Data data = Data.fromMap(snapshot.data.data());
if (data.hasPickup) {
return DataPage(data:data);
}
else{
return Scaffold(
body: Container(
),
);
}
}
return Scaffold(
body: Center(
child: CircularProgressIndicator(),
),
);
},
)
: Scaffold(
body: Center(
child: CircularProgressIndicator(),
),
);
}
}
关于如何更改代码以使其工作的任何建议?谢谢:)
data.hasPickup
的值对于某些值可能为 null,这是导致此错误的原因,为了解决此“症状”,您应该在构建器中进行 if (data.hasPickup != null)
检查并这将得到缓解。
也就是说,问题的真正根本原因不在这里,但是当您创建这些文档时,避免此类问题的最佳做法是永远不要使用 null
初始化 Firestore 值值(或至少将被积极使用的值),而是使用默认值,例如 false
.
当我运行下面的代码时,我得到如下所示的错误消息:
======== Exception caught by widgets library ======================================================= The following assertion was thrown building StreamBuilder(dirty, state: _StreamBuilderBaseState<DocumentSnapshot, AsyncSnapshot>#cfcf8): Failed assertion: boolean expression must not be null
我运行的代码如下图:
class DataState extends AppState {
final CollectionReference dataCollection =
FirebaseFirestore.instance.collection('data');
Stream<DocumentSnapshot> dataStream({String uid}) =>
dataCollection.doc(uid).snapshots();
class WaitingPage extends StatefulWidget {
@override
_WaitingPageState createState() => _WaitingPageState();
}
class _WaitingPageState extends State<WaitingPage> {
@override
Widget build(BuildContext context) {
final AuthState userProvider = Provider.of<AuthState>(context);
var dataState = Provider.of<DataState>(context, listen:false);
return (userProvider.userModel != null)
? StreamBuilder<DocumentSnapshot>(
stream: dataState.dataStream(uid: userProvider.userModel.userId),
builder: (context, snapshot) {
if (snapshot.hasData && snapshot.data.data() != null) {
Data data = Data.fromMap(snapshot.data.data());
if (data.hasPickup) {
return DataPage(data:data);
}
else{
return Scaffold(
body: Container(
),
);
}
}
return Scaffold(
body: Center(
child: CircularProgressIndicator(),
),
);
},
)
: Scaffold(
body: Center(
child: CircularProgressIndicator(),
),
);
}
}
关于如何更改代码以使其工作的任何建议?谢谢:)
data.hasPickup
的值对于某些值可能为 null,这是导致此错误的原因,为了解决此“症状”,您应该在构建器中进行 if (data.hasPickup != null)
检查并这将得到缓解。
也就是说,问题的真正根本原因不在这里,但是当您创建这些文档时,避免此类问题的最佳做法是永远不要使用 null
初始化 Firestore 值值(或至少将被积极使用的值),而是使用默认值,例如 false
.