0

I am trying to access the 'title' from the following list but it keeps throwing error.

    var moviesDB = {
      "genres": [
        "Comedy",
        "Fantasy",
        "Crime",
        "Drama",
        "Music",
        "Adventure",
        "History",
        "Thriller",
        "Animation",
        "Family",
        "Mystery",
        "Biography",
        "Action",
        "Film-Noir",
        "Romance",
        "Sci-Fi",
        "War",
        "Western",
        "Horror",
        "Musical",
        "Sport"
      ],
      "movies": [
        {
          "id": 1,
          "title": "Beetlejuice",
          "year": "1988",
          "runtime": "92",
          "genres": ["Comedy", "Fantasy"],
          "director": "Tim Burton",
          "actors": "Alec Baldwin, Geena Davis, Annie McEnroe, Maurice Page",
          "plot":
              "A couple of recently deceased ghosts contract the services of a \"bio-exorcist\" in order to remove the obnoxious new owners of their house.",
          "posterUrl":
              "https://images-na.ssl-images-amazon.com/images/M/MV5BMTUwODE3MDE0MV5BMl5BanBnXkFtZTgwNTk1MjI4MzE@._V1_SX300.jpg"
        },
        {
          "id": 2,
          "title": "The Cotton Club",
          "year": "1984",
          "runtime": "127",
          "genres": ["Crime", "Drama", "Music"],
          "director": "Francis Ford Coppola",
          "actors": "Richard Gere, Gregory Hines, Diane Lane, Lonette McKee",
          "plot":
              "The Cotton Club was a famous night club in Harlem. The story follows the people that visited the club, those that ran it, and is peppered with the Jazz music that made it so famous.",
          "posterUrl":
              "https://images-na.ssl-images-amazon.com/images/M/MV5BMTU5ODAyNzA4OV5BMl5BanBnXkFtZTcwNzYwNTIzNA@@._V1_SX300.jpg"
        },
      ]
    }

I can go as far as moviesDB["movies"][0] but cannot get the title property. Although I can do the same in Javascript and it works with no errors.

console.log(moviesDB["movies"][0]["title"]);

Any solution for this?

3
  • console.log((moviesDB['movies'][0] as Map<String, dynamic>)['title']); Commented Oct 28, 2020 at 14:58
  • thanks, but why do we need to cast it in order to access the data? I have no idea. Can you explain Commented Oct 28, 2020 at 15:25
  • 1
    so that flutter can know which type the data is in. Commented Oct 28, 2020 at 15:45

1 Answer 1

1

You need to make a cast on the element of your movie list.

print((moviesDB['movies'][0] as Map<String, dynamic>)['title']);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for that. But why do we need to cast it instead of directly using the JS way? Is that not supported ?
Dart is a strong typed language, it is type safe. When you are accessing your moviesDB it is considered as a Map<String, dynamic> so when trying to access an element of your list it doesn't know which type of object it is as there is multiple object that can access data using [0] so it is considered as an Object and you need to cast it to go deeper in your data tree.

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.