gpt4 book ai didi

dart - 在 Dart 中将对象插入数组

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

  List returnMovies = [];

Future<List> _getData() async {

final response = await http.get("https:../getTodayMovies",
headers: {HttpHeaders.AUTHORIZATION: Acess_Token.access_token});


if (response.body != null) {
returnMovies = json.decode(response.body);
.....

setState(() {});
} else {
final responseUpcoming = await http.get("/upcoming",
headers: {HttpHeaders.AUTHORIZATION: Acess_Token.access_token});
returnMovies = json.decode(responseUpcoming.body);


}

response.body 看起来像:

[{"id":394470548,"host_group_name":"heyab redda","movie_image":"..png","type":"horror","code":"X123","name":"Lovely","start_time":1554364800,"end_time":1554393600,"}]

responseUpcoming.body 看起来像:

 {"id":394470545,"host_group_name":"foo redda","movie_image":".png","type":"horror","code":"X123","name":"Lovely","start_time":1554364800,"end_time":1554393600,"}, {"id":394470548,"host_group_name":"foo1 redda","movie_image":"..png","type":"comic","code":"X125","name":"Lovely1","start_time":1554364800,"end_time":1554393600,"}

我得到的错误是:String, dynamic is not a subtype of type List<dynamic> .

在我执行的第一个 API 调用中,我通常返回一个对象数组,但是,当它为空时,第二个 API 调用返回一个对象列表,我想将这些对象插入名为 returnMovies 的数组中,如何我能做到吗??有什么办法可以将这些对象推送到数组中吗?那么我想使用这个数组来动态构建一个Listview.builder。我声明的方式是否正确?我对 Dart 很陌生。谢谢

最佳答案

听起来你正在寻找 addAll

returnMovies.addAll(json.decode(returnUpcoming.body))

关于dart - 在 Dart 中将对象插入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54750365/

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