Flutter type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>'

574

You have incorrectly cast the "USDBRL" as a List, when it is a Map.

Get rid of this line: List<dynamic> body = json["USDBRL"];

and replace with this line: Map<String, dynamic> body = json["USDBRL"];

That should resolve the casting error you are seeing.

To resolve the toList error, you need to change how you are getting the Dolar.

Dolar dolar = Dolar.fromJson(body);

This is because the "USDBRL" does not contain a list of items. It is one object with properties and values. I'm assuming that those values inside "USDBRL" are what you are wanting to use to configure the data in the Dolar object. So you just change it to be a single instance of Dolar that gets it's data from the "USDBRL" Map.

The final code could look something like this:

Future<Dolar> getDolar() async {
    try {
        Response res = await get(Uri.parse(endPointUrl));
        if (res.statusCode==200) {
            final json = jsonDecode(res.body);
            Dolar dolar = Dolar.fromJson(json["USDBRL"] as Map<String, dynamic>);
            return dolar;
        } else {
            throw ("Can't access the Dolar API");
        }
    } catch (e) {
        print(e);
        rethrow;
    }
}
Share:
574
Admin
Author by

Admin

Updated on January 04, 2023

Comments

  • Admin
    Admin over 1 year

    I'm a Flutter language learner, new to this world,

    Code Example 2º Error I have a json like:

    {
      "USDBRL":
      {
        "id": "...",
        "name": "...",...
      }
    }
    

    My Dolar Model:

    class Dolar {
      String param1;
      String param2;
    
      // Constructor
      Dolar({
        required this.param1,
        required this.param2,
      });
    
      factory Dolar.fromJson(Map<String, dynamic> json) {
        return Dolar(
          param1: json['param1'],
          param2: json['param2'],
        );
      }
    }
    

    I need to grab all "USDBRL" fields, but when I run the app I get "flutter: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>' ".

    I've tried searching for resolutions on the internet, but none of the alternatives I've tried have worked.

  • Admin
    Admin about 2 years
    Error: The method 'toList' isn't defined for the class 'Map<dynamic, dynamic>'. lib/services/api_service.dart:21 - 'Map' is from 'dart:core'. Try correcting the name to the name of an existing method, or defining a method named 'toList'. body.map((dynamic item) => Dolar.fromJson(item)).toList(); ^^^^^^
  • daddygames
    daddygames about 2 years
    You may need to update your question to include the Dolar model and see the toList function that you are attempting to access.
  • Admin
    Admin about 2 years
    I put an example equal to my Model and a print of the current error after your suggestion
  • daddygames
    daddygames about 2 years
    Are you attempting to get the Dolar data out of the "USDBRL" object?
  • Admin
    Admin about 2 years
    Yes, all of them, id, name, as it is in the json template I put above, to put them in my model, which has the same fields
  • daddygames
    daddygames about 2 years
    Updated the answer to address the issue
  • Admin
    Admin about 2 years
    I didn't understand, sorry
  • daddygames
    daddygames about 2 years
    I updated the answer again to try to clarify. It includes a representation of what your code could potentially look like combining the concepts I have tried to explain.
  • Admin
    Admin about 2 years
    Thank you very much, my problem was solved at first.
  • Admin
    Admin about 2 years
    The answer from daddy Games above already solved my problem, I also appreciate your help :D