gpt4 book ai didi

list - 从 For 语句向 Flutter 中的列表添加元素?

转载 作者:IT王子 更新时间:2023-10-29 06:35:57 28 4
gpt4 key购买 nike

我在尝试将我的 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.addsearchIds.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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com