这个错误通常意味着API在期望返回一个列表的数据时返回了一个空值null。因此,在使用返回的数据之前,我们应该先检查它是否为空,然后再进行处理。
以下是一个例子:
Future> fetchQuotes() async {
final response = await http.get('https://example.com/quotes');
if (response.statusCode == 200) {
// 若返回的数据为null则返回一个空的列表
if(response.body == null){
return List();
}
final List parsed = jsonDecode(response.body);
return parsed.map((json) => Quotes.fromJson(json)).toList();
} else {
throw Exception('Failed to fetch quotes');
}
}