添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

_CastError (type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>' in type cast)

Ask Question

Want to ask again ... try implementing the data API stored in a local database ... the form of the JSON API The object in which there is a JSON Array ... there was an error when wanting to load data from the API to Local database ... roughly this is an error because of "the wrong model or what

Api Service

class MealApiProvider {
  Future<List<Categories>> getAllMeal() async {
    var url = "https://www.themealdb.com/api/json/v1/1/categories.php";
    Response response = await Dio().get(url);
    print("Hasil Respon ${response.data}");
    return (response.data as List).map((employee) {
      print('Inserting $employee');
      DBProvider.db.insertMeals(Categories.fromJson(employee));
    }).toList();

Model

class DataMeal {
  final List<Categories> categories;
  DataMeal({this.categories});
  @override
  String toString() {
    return 'DataMeal{categories: $categories}';
  factory DataMeal.fromJson(Map<String, dynamic> json) {
    return DataMeal(
      categories: List<Categories>.from(
        json["categories"].map(
          (categories) {
            return Categories.fromJson(categories);
  Map<String, dynamic> toJson() => {
        "categories": List<dynamic>.from(
          categories.map(
            (x) => x.toJson(),

Local DB

  initDB() async {
    Directory documentsDirectory = await getApplicationDocumentsDirectory();
    final String path = join(documentsDirectory.path, '$nameDatabase');
    print('insert database $path');
    return await openDatabase(path, version: 1, onOpen: (db) {},
        onCreate: (Database db, int version) async {
      await db.execute('CREATE TABLE $nameTable('
          'idCategory,'
          'strCategory TEXT,'
          'strCategoryThumb TEXT,'
          'strCategoryDescription TEXT'
          ')');
  insertMeals(DataMeal newMeal) async {
    await deleteAllMeal();
    final Database db = await database;
    final res = await db.insert("$nameTable", newMeal.toJson());
    print("inserting data $res");
    return res;

Error

return (response.data as List).map((employee)

Respon Data

Hey you're using wrong data type for array use List<dynamic> for array and use Map<String,dynamic> for JSON Object. Try it out it will work. – Vrushi Patel Feb 19, 2020 at 6:08
class MealApiProvider {
  Future<List<Categories>> getAllMeal() async {
    var url = "https://www.themealdb.com/api/json/v1/1/categories.php";
    Response response = await Dio().get(url);
    print("Hasil Respon ${response.data}");
    //***Change below line****
    return (response.data['categories'] as List).map((employee) {
      print('Inserting $employee');
      DBProvider.db.insertMeals(Categories.fromJson(employee));
    }).toList();

instead of

class MealApiProvider {
  Future<List<Categories>> getAllMeal() async {
    var url = "https://www.themealdb.com/api/json/v1/1/categories.php";
    Response response = await Dio().get(url);
    print("Hasil Respon ${response.data}");
    //*** I change the below line !
    return (response.data as List).map((employee) {
      print('Inserting $employee');
      DBProvider.db.insertMeals(Categories.fromJson(employee));
    }).toList();
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.