[英]Null check operator used on a null value
body: StreamBuilder(
stream: db.collection('products').where('category', isEqualTo: 'category').snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
if(snapshot.hasError){
Fluttertoast.showToast(msg: 'Something error');
}
if(snapshot.connectionState == ConnectionState.waiting) {
CircularProgressIndicator();
}
final values = snapshot.data!.docs;
return ListView.builder(
itemCount: values!.length,
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(values![index]['name']),
),
),
);
}
);
},
),
The following _CastError was thrown building StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(dirty, state: _StreamBuilderBaseState<QuerySnapshot<Map<String, dynamic>>, AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>>>#c87c1):
构建 StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(脏,state:_StreamBuilderBaseState<QuerySnapshot<Map<String, dynamic>>, AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>>>#c87c1 引发了以下 _CastError ):
you missed return
in your if
blocks, so those widgets are not being returned, this redirect to the snapshot.data..docs
widget which is null
initially, change it to this:你错过了你的
if
块中的return
,所以那些小部件没有被返回,这重定向到snapshot.data..docs
小部件,它最初是null
,将它更改为:
body: StreamBuilder(
stream: db.collection('products').where('category', isEqualTo: 'category').snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
if(snapshot.hasError){
Fluttertoast.showToast(msg: 'Something error');
return Text("error"); // add this to showcase the error
}
if(snapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator(); // add this to show loading when data is fetching
}
final values = snapshot.data!.docs;
return ListView.builder(
itemCount: values!.length,
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(values![index]['name']),
),
),
);
}
);
},
),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.