Dart NoSuchMethodError输出指的是什么闭包?

我有一个Flutter应用(v1.2.1),我正在尝试调试一个错误,该错误无法在调试模式下重现。我知道根本原因是在空对象上调用forEach,但我正在尝试查找代码中所指的forEach。我不理解错误消息的“尝试调用”部分,并且在Dart NoSuchMethodError类文档中找不到任何有关它的详细信息。这是错误:

NoSuchMethodError: The method 'forEach' was called on null.
Receiver: null
Tried calling: forEach(Closure: (CustomObject) => Null)

该错误是否表示它发生在格式为forEach((CustomObject)=> {method body})的forEach或位于格式(CustomObject)=> {method body}的闭包内部的forEach中?

例如会是这样吗?

methodOne(List<CustomObject> fakeObjects) {
 fakeObjects.forEach((CustomObject thingOne) {
   print(thingOne);
 });
}

或类似的内容:

methodTwo(CustomObject thingOne) {
  ...some code
  listOfItems.forEach((String item) {
    print(item);
  });
}
sixupiaofubud 回答:Dart NoSuchMethodError输出指的是什么闭包?

我已重现此错误。
您的fakeObjects为null,请参见以下代码段和图片

List<String> fakeObjects = null;
    fakeObjects.forEach((String thingOne) {
      print(thingOne);
    });

enter image description here

输出

The method 'forEach' was called on null.
Receiver: null
Tried calling: forEach(Closure: (String) => Null)
本文链接:https://www.f2er.com/3141574.html

大家都在问