Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push objects into array in Dart

Tags:

flutter

dart

  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);


    }

The response.body looks like:

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

The responseUpcoming.body looks like:

 {"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,"}

The error I get is: String, dynamic is not a subtype of type List<dynamic>.

In the first API call that I am doing I normally get in return an array of objects, however, when this is empty, the second API call returns a list of objects that I want to push into the array called returnMovies, how can I achieve this?? Is there any way to .push these objects in the array?? So then I want to use this array to build dynamically a Listview.builder. Also is it correct the way I am declaring it? I am quite new on Dart. Thank you

like image 711
heyr Avatar asked Feb 18 '19 15:02

heyr


2 Answers

I will suggest to use

returnMovies.addAll({your object here})
like image 176
Mohamed hassan kadri Avatar answered Sep 16 '22 12:09

Mohamed hassan kadri


Sounds like you are looking for addAll

returnMovies.addAll(json.decode(returnUpcoming.body))
like image 22
Günter Zöchbauer Avatar answered Sep 18 '22 12:09

Günter Zöchbauer