Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type 'List<dynamic>' is not a subtype of type 'List<int>' where

I am new to Flutter, I try to run one GitHub project but got an error like:

type List dynamic is not a subtype of type List int where.

Github Link

Error Line

List<int> genreIds;

MediaItem._internalFromJson(Map jsonMap, {MediaType type: MediaType.movie})
      :
        type = type,
        id = jsonMap["id"].toInt(),
        voteAverage = jsonMap["vote_average"].toDouble(),
        title = jsonMap[(type == MediaType.movie ? "title" : "name")],
        posterPath = jsonMap["poster_path"] ?? "",
        backdropPath = jsonMap["backdrop_path"] ?? "",
        overview = jsonMap["overview"],
        releaseDate = jsonMap[(type == MediaType.movie
            ? "release_date"
            : "first_air_date")],

        genreIds = jsonMap["genre_ids"];//in this line

}

Above code File

like image 305
Magesh Pandian Avatar asked May 09 '18 03:05

Magesh Pandian


3 Answers

Change

genreIds = jsonMap["genre_ids"];

to

genreIds = jsonMap["genre_ids"].cast<int>();

types in JSON maps or lists don't have concrete generic types. genreIds requires a List<int> not a List (or List<dynamic>), therefore you need to bring the value to its required type before you can assign it.

If you haven't seen this error earlier for the same code, then it's probably because you upgraded to a Dart version where --preview-dart-2 became the default (it was opt-in previously)

like image 114
Günter Zöchbauer Avatar answered Nov 11 '22 04:11

Günter Zöchbauer


A more elegant way could also be initialising new List instead of casting.

var genreIdsFromJson = jsonMap['genre_ids'];
List<int> genreIdsList = new List<int>.from(genreIdsFromJson);

// then you can use gendreIdsList to the mapping function
// ...

gendreIds = genreIdsList
...

Update: As per the documentation

`` All the elements should be instances of E. The elements iterable itself may have any element type, so this constructor can be used to down-cast a List, for example as:

List<SuperType> superList = ...;
List<SubType> subList =
    new List<SubType>.from(superList.whereType<SubType>());

This constructor creates a growable list when growable is true; otherwise, it returns a fixed-length list. ``

Update: Why is it better to initialize than to cast? (Pros / Cons)

Explicit Conversions (Casting): The process is usually associated with information loss or failure to convert between types

Creating new immutable elements is better than casting. Revealing type-related bugs at compile time, more readable code, more maintainable code, better ahead of time (AOT) compilation.

i.e. it is better to try parse or parse a value with predefined method because if the type does not match the value will be null. On the other hand explicitly casting an object or value can throw errors at runtime.

like image 51
Edi Avatar answered Nov 11 '22 04:11

Edi


A shorter way to handle is

genreIds = (jsonMap["genre_ids"] as List)?.map((e) => e as int)?.toList();
like image 6
thien nguyen Avatar answered Nov 11 '22 05:11

thien nguyen