未处理的异常:NoSuchMethodError:在 null 上调用了 getter 'length'。不知道原因

时间:2021-02-06 17:52:13

标签: flutter compiler-errors

我知道这个错误已经有人多次回答了,但我认为我的情况有点不同。到目前为止,我的颤振运行良好,没有任何困难。但是突然它在应用程序运行期间开始给我这个错误。它在安装时没有在应用程序上打开。这个错误只是在应用程序安装过程中不断出现,我不知道它来自哪里。enter image description here

请指导我如何找到问题的位置以及如何解决。

1 个答案:

答案 0 :(得分:1)

这个错误可能是因为您给 jsonDecode 提供了一个空值:

var jsonString = null;
var parsed = jsonDecode(jsonString);
print(parsed.length);
// NoSuchMethodError: The getter 'length' was called on null.

在您的项目中搜索 jsonDecodejson.decode 并且当 jsonString 为 null 时不要调用它并显示错误消息,或将其替换为默认的 jsonString:

var jsonString = null;
const defaultJson = '{}';
var parsed = jsonDecode(jsonString ?? defaultJson);
print(parsed.length);
// 0