The method 'add' can't be unconditionally invoked because the receiver can be 'null'

huangapple go评论61阅读模式
英文:

The method 'add' can't be unconditionally invoked because the receiver can be 'null'

问题

以下是您要翻译的内容:

"我正在尝试处理一个Flutter应用程序的一些数据,但是我的代码中出现了以下错误:

方法 'add' 无法无条件调用,因为接收方可以为 'null'。
尝试使调用条件化(使用 '?.') 或向目标添加空检查('!')。"

基本上,我有一个以字符串为键,以List<SourcefulListing>为值的Map。对于每个循环,应该将SourcefulListing对象添加到Map中,但在.add方法上出现了错误。任何帮助将不胜感激。

英文:

I'm trying to handle some data for a flutter application, however I am getting the following error on my code:

The method &#39;add&#39; can&#39;t be unconditionally invoked because the receiver can be &#39;null&#39;.
Try making the call conditional (using &#39;?.&#39;) or adding a null check to the target (&#39;!&#39;).

Map&lt;String, List&lt;SourcefulListing&gt;&gt; sortedSkills = {};
    QuerySnapshot listingSnapshot = await listingsRef.get();
    List&lt;SourcefulListing&gt; listings = [];
    for (int i = 0; i &lt; listingSnapshot.docs.length; i++) {
      listings.add(SourcefulListing.fromJson(
          listingSnapshot.docs[i].data() as Map&lt;String, dynamic&gt;));
    }
    for (String skill in skills) {
      for (SourcefulListing listing in listings) {
        if (listing.selectedSkill == skill) {
          if (sortedSkills[skill] == null || sortedSkills[skill] != []) {
            sortedSkills[skill] = [listing];
          } else {
            sortedSkills[skill] = sortedSkills[skill].add(listing);
          }
        }
      }
    }

Basically I have a Map with Strings as key and List<String> for the values. The for each loop should add the SourcefulListing object to the map, however there is an error on the .add method.
Any help would be much appreciated.

答案1

得分: 1

以下是翻译好的代码部分:

尝试这个,

Map<String, List<SourcefulListing>> sortedSkills = {};
QuerySnapshot listingSnapshot = await listingsRef.get();
List<SourcefulListing> listings = [];
for (int i = 0; i < listingSnapshot.docs.length; i++) {
  listings.add(SourcefulListing.fromJson(
      listingSnapshot.docs[i].data() as Map<String, dynamic>));
}
for (String skill in skills) {
  for (SourcefulListing listing in listings) {
    if (listing.selectedSkill == skill) {
      if (sortedSkills[skill] == null || sortedSkills[skill] != []) {
        sortedSkills[skill] = [listing];
      } else {
        sortedSkills[skill]?.add(listing); // 在这里进行了更改
        setState(() {}); // 更新UI
      }
    }
  }
}

空安全信息:https://dart.dev/null-safety

英文:

Try this,

  Map&lt;String, List&lt;SourcefulListing&gt;&gt; sortedSkills = {};
QuerySnapshot listingSnapshot = await listingsRef.get();
List&lt;SourcefulListing&gt; listings = [];
for (int i = 0; i &lt; listingSnapshot.docs.length; i++) {
  listings.add(SourcefulListing.fromJson(
      listingSnapshot.docs[i].data() as Map&lt;String, dynamic&gt;));
}
for (String skill in skills) {
  for (SourcefulListing listing in listings) {
    if (listing.selectedSkill == skill) {
      if (sortedSkills[skill] == null || sortedSkills[skill] != []) {
        sortedSkills[skill] = [listing];
      } else {
        sortedSkills[skill]?.add(listing); // changes made here
         setState(() {}); // update ui
      }
    }
  }
}

Null Safety : https://dart.dev/null-safety

huangapple
  • 本文由 发表于 2023年2月16日 13:21:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468140.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定