17
  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

3 Answers 3

13

Sounds like you are looking for addAll

returnMovies.addAll(json.decode(returnUpcoming.body))
Sign up to request clarification or add additional context in comments.

3 Comments

Yes I actually do, however, I get error: <String, dynamic>' is not a subtype of type 'Iterable<dynamic>.
You can't add a Map (key-value pairs) to an Iterable (list of values). You would need to provide more details what you want to accomplish (how the data in returnMovies and returnUpcoming.body looks like exactly and what the resulting returnMovies should look like)
I just edited my post, with the potential response of my API call and how I would like to use its data, thanks.
5

I will suggest to use

returnMovies.addAll({your object here})

Comments

2

When you do this json.decode(response.body) you are getting a List of Map you should use List<dynamic> movieListData and get the items like this:

    movieListData = json.decode(response.body);
    returnMovies = movieListData.map((dynamic movieData) {
          String id = movieData['_id'];
          String host_group_name = movieData['host_group_name'];
          String duration = movieData['duration'];
          return new Movie(id,title, duration);
        }).toList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.