0
我收到以下錯誤,同時試圖從我的for循環元素添加到我的List
...將元素添加到來自For語句的Flutter中?
NoSuchMethodError: The method 'addAll' was called on null.
Receiver: null
Tried calling: addAll("LrWr826cd3Y")
這裏是我的代碼...
Future getData() async {
//Map videoId;
String url = 'https://Youtube API';
var httpClient = createHttpClient();
var response = await httpClient.read(url);
Map data = JSON.decode(response);
var videos = data['items']; //returns a List of Maps
List searchTitles;
List searchIds;
List searchImages;
for (var items in videos) {
//iterate over the list
Map myMap = items; //store each map
final video = (myMap['id'] as Map);
print(video['videoId']);
searchIds.addAll(video['videoId']);
final details = (myMap['snippet'] as Map);
final videoimage = (details['thumbnails'] as Map);
final medium = (videoimage['medium'] as Map);
}
setState(() { });
if (!mounted) return;
}
print(video['videoId']);
成功地列出了3 Youtube視頻ID作爲字符串。 searchIds.addAll(video['videoId']);
引發錯誤。我試過searchIds.add
和searchIds.addAll
。我哪裏錯了?
我想這些名單最終推到我這裏List類..
class CardInfo {
//Constructor
List id;
List title;
List video;
CardInfo.fromJson(List json) {
this.id;
this.title;
this.video;
}
}
這是我如何得到它爲Flutter工作...'List searchIds = new List();' –
我看你的問題是固定的。做得好。 –