skip to Main Content

I would like to use my JSON file to store all the info in it to display in Flutter.
Everytime i get this Error:
_TypeError (type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>')

Here are my Files:
db.json
breed_page.dart

Can someone help me solving this error or show me a better way to do this?

Normally, the name and image should be read from the JSON file and then displayed in the list tile as a circle image or as text. I have also checked my JSON file and tried other functions like this one but nothing helped. By the way I am a newbie in Flutter

ListView.builder(
  itemCount: breeds.length,
  itemBuilder: (BuildContext context, int index) {
    return ListTile(
      leading: Image.network(breeds[index]['img']),
      title: Text(breeds[index]['name']),
      onTap: () {
        // TODO: Navigate to breed details page
      },
    );
  },
);

2

Answers


  1. json.decode(jsonString); seems to be converting the data from String to Map<String, dynamic> instead of List<dynamic>

    Please go through the language docs to check if you’re using the right return type for your json
    https://api.dart.dev/stable/2.19.3/dart-convert/jsonDecode.html

    Login or Signup to reply.
  2. your decoded json string is Map<String,dynamic> not a List<dynamic>

    {
        "breeds":[
           {
    

    the List are a value from key breeds

    Workaroud:

      Future<List<dynamic>> fetchBreeds() async {
        String jsonString = await rootBundle.loadString('lib/db.json');
       final tempDecoded = json.decode(jsonString);
       // now get the list with key breeds
       final breeds = tempDecoded["breeds"];
        return breeds;
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search