简体   繁体   English

Null 检查用于 null 值的运算符

[英]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.

相关问题 Null 检查运算符用于 null 值 FirebaseFirestore - Null check operator used on a null value FirebaseFirestore flutter 中的错误“用于 null 值的空检查运算符” - Error "Null check operator used on a null value" in flutter Flutter Firebase - Null 检查运算符用于 null 值上的字段 - Flutter Firebase - Null check operator used on a null value, can't get Field Null 检查运算符用于 Stream 构建器中的 null 值更改通知提供程序错误 - Null check operator used on a null value in Stream builder in Change Notifier Provider error FirebaseCloudMessaging: PlatformException (PlatformException(null-error, Host platform returned null value for non-null return value., null, null)) - FirebaseCloudMessaging : PlatformException (PlatformException(null-error, Host platform returned null value for non-null return value., null, null)) 未处理的异常:PlatformException(空错误,主机平台为非空返回值返回 null 值。,null,空) - Unhandled Exception: PlatformException(null-error, Host platform returned null value for non-null return value., null, null) 如何在 Firebase 中为 Flutter 检查 null 当前用户 - How to check for null current user in Firebase for Flutter 将 nulll 值与非 null 值 SQL 进行比较 - Compare nulll value with non null value SQL 将 Firebase 用户 photoURL 值设置为 null - Setting a Firebase User photoURL value to null 必须返回非空值,因为返回类型“UserCredentialPlatform”不允许空值 - A non-null value must be returned since the return type 'UserCredentialPlatform' doesn't allow null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM