'Flutter - Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'

I'm trying to get an api, I can print the api response in the console. However, when I'm receive the respond to my model class, console shows and error like this

E/flutter ( 9292): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'

model.dart

class CategoryDishes {
  final String dishId;
  final String dishName;
  final String dishDescription;

  CategoryDishes(
      {this.dishId,
      this.dishName,
      this.dishDescription,});

  factory CategoryDishes.fromJson(Map<String, dynamic> json) {
    return CategoryDishes(
        dishId: json['dish_id'],
        dishName: json['dish_name'],
        dishDescription: json['dish_description'],

  }

  static Resource<List<CategoryDishes>> get all {
    return Resource(
        url: Constants.FOOD_API_URL,
        parse: (response) {
          final result = json.decode(response.body.toString());
          print(response);
          Iterable list = result['category_dishes'];
          return list.map((model) => CategoryDishes.fromJson(model)).toList();
        });
  }
}

web_service.dart

class Resource<T> {
  final String url;
  T Function(Response response) parse;

  Resource({this.url, this.parse});
}

class Webservice {
  Future<T> load<T>(Resource<T> resource) async {
    final response = await http.get(resource.url);
    if (response.statusCode == 200) {
      debugPrint("----D------>" + response.body);
      return resource.parse(response);
    } else {
      throw Exception('Failed to load data!');
    }
  }
}

The debugPrint showing the api in console, but also the above error showing and the api data not showing on my view that I've created.

What I did wrong?

Any suggestion would be helpful.



Solution 1:[1]

The API returning JSON Array ! Try like below !

static Resource<List<CategoryDishes>> get all {
    return Resource(
        url: Constants.FOOD_API_URL,
        parse: (response) {
            final result = json.decode(response.body.toString());
            print(response);
        //added 0 indexex, so it gets 1st element of JSON Arrays
            Iterable list = result[0]['table_menu_list'][0]['category_dishes'];
            return list.map((model) => CategoryDishes.fromJson(model)).toList();
        });
    }
}

Solution 2:[2]

When the API returns an array we can't use

 result['category_dishes'] 

Instead we use the result directly since it is a list

List list= result ;
list.map((i) =>.....

I got the same error and this was the solution for me.

Solution 3:[3]

I was getting this error from firebase because I was not putting the field values in quotes. They must be put in quotes to work correct.

products.json?auth=token&orderBy=CreatorID <- Error

Simply put the field values in quotes

products.json?auth=token&orderBy="CreatorID" <- Resolved

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tyler2P
Solution 2 Tareq
Solution 3 Hammad Khan