【问题标题】:Error:The method '[]' can't be unconditionally invoked because the receiver can be 'null'错误:无法无条件调用方法“[]”,因为接收者可以为“null”
【发布时间】:2021-08-10 02:49:00
【问题描述】:

我只是将我的项目转换为 Null Safety,但我收到错误提示

The method '[]' can't be unconditionally invoked because the receiver can be 'null'. Try making the call conditional (using '?.') or adding a null check to the target ('!').   

我有点困惑,我不知道该怎么办。

 return ListView.builder(
                                    scrollDirection: Axis.horizontal,
                                    itemCount: snapshot.data['Interest'].length ,// i am getting error here
                                    itemBuilder: (context, index) {
                                      return Padding(
                                        padding: const EdgeInsets.only(top: 12.0),
                                        child: bottomCardList(
                                            'assets/1 (6).jpeg',
                                            snapshot.data['Interest'][index]// i am getting error here
                                                .toString(),
                                            () {}),
                                      );
                                    });
                              }),

谢谢

【问题讨论】:

  • snapshot.data['Interest'] 可以为空,所以也许你可以这样检查:snapshot.data['Interest'] != null ? snapshot.data['Interest'].length : 0
  • @JorgeVieira 仍然给了我错误

标签: flutter dart dart-null-safety


【解决方案1】:

问题:

从可为空类型的映射(即Map?)中检索值时会出现此错误。假设你有:

Map? map;

你正在访问它

int i = map['0']; // <-- Error

解决方案:

  1. 提供默认值(推荐)

    int i = map?['0'] ?? -1;
    
  2. 如果您确定该值不是 null,请使用 Bang 运算符。

    int i = map!['0'];
    

【讨论】:

    【解决方案2】:

    发生的情况是,在切换到 null 安全之后,您不能直接使用 可以为 null 的变量编写语句,而不检查它们是否为 null。在这种情况下,变量 snapshot.data 可以为 null,因此您必须相应地编写代码。尝试将您的代码转换为:

    return ListView.builder(
      scrollDirection: Axis.horizontal,
      itemCount: snapshot.data!['interest'].length,
      // i am getting error here
      itemBuilder: (context, index) {
        return Padding(
          padding: const EdgeInsets.only(top: 12.0),
          child: bottomCardList(
            'assets/1 (6).jpeg',
            snapshot.data?['Interest'][index] // i am getting error here
                .toString(),
          ),
        );
      },
    );
    

    现在,有了这个,您的 itemCount 错误应该消失(如果没有,请更新您的 cloud_firestore 插件,它不是空安全的)。至于您在bottomCardList 遇到的错误,这取决于您的bottomCardList 参数是否为空安全。如果bottomCardList 的类型为bottomCardList(String someVar, String someOtherVar),您可以将其更改为bottomCardList(String someVar, String? someOtherVar)。然后,在你的 bottomCardList 代码中,你必须确保你正在处理 someOtherVar 可以为空的情况。

    您可以观看此视频了解更多信息:https://www.youtube.com/watch?v=iYhOU9AuaFs


    编辑 对于“未定义对象错误”:

    我假设您的 builder 参数类似于:

    builder: (context, snapshot) {
        return ListBuilder etc. etc. 
    }
                    
    

    尝试将其更改为:

    builder: (context, AsyncSnapshot<DocumentSnapshot<Map<String, dynamic>>> snapshot) {
        return ListBuilder etc. etc. 
    }
    

    并且错误应该消失。 DocumentSnapshot 是如果您正在执行 collection.docs.snapshots()。如果您正在执行 collection.snapshots(),请使用 QuerySnapshot 等。

    【讨论】:

    • snapshot.data!['interest'].length... 我以前试过这个,但仍然显示错误
    • snapshot.data?['Interest'][index] // 我在这里遇到错误 .toString(),即使这样。我试过这个仍然显示错误
    • 错误到底说了什么?还是和以前一样的错误吗?
    • 没有为“object”类型定义操作“[]”
    【解决方案3】:

    我通过给 StreamBuilder 一个类型解决了这个问题。

    StreamBuilder<DocumentSnapshot<Map>>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 2021-09-05
      • 2021-10-05
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      • 2021-08-27
      相关资源
      最近更新 更多