【发布时间】:2021-08-22 22:40:41
【问题描述】:
我在 2 个字符串列表中添加数据,但在它们显示 0 长度的方法之外。但在 fetchList 方法中,它们显示 1643 长度。
fetchList() async {
setState(() {
isLoading=true;
});
var response = await http.get(Uri.parse("https://type.fit/api/quotes"));
var data = json.decode(response.body);
if (response.statusCode == 200) {
for (var i in data) {
var response2 = await http.get(Uri.parse(
"https://api.unsplash.com/search/photos?client_id=$unsplashAPIKey&query=${i['author']}"));
quoteList.add(
QuoteListClass(
quote: i['text'] != null ? i['text'] : 'Anonymous',
author: i['author'] != null ? i['author'] : '',
image: response2.statusCode == 200
? json.decode(response2.body)['results'][0]['urls']['raw']
: '',
),
);
print(json.decode(response2.body)['results'][0]['urls']['raw']);
}
} else {
throw Exception('Failed to load quotes');
}
setState(() {
isLoading = false;
});
}
@override
void initState() {
super.initState();
Future.delayed(Duration.zero,()async{
this.fetchList();
});
}
现在有一个新错误。
E/flutter (9830): [错误:flutter/lib/ui/ui_dart_state.cc(186)] 未处理的异常:RangeError(索引):无效值:有效值 范围为空:0 E/flutter(9830):#0 List.[] (dart:core-patch/growable_array.dart:254:60)
【问题讨论】: