尝试将for循环中的元素添加到List
时收到以下错误...
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;
}
}
最佳答案
您没有实例化searchIds对象。加上这个
List searchIds = new ArrayList<>();
(要么)
List searchIds = new List();
关于list - 从For语句向Flutter中的列表添加元素?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46376691/