Maszgalang
Maszgalang

Reputation: 79

type 'Null' is not a subtype of type 'Map<String, dynamic>'

hi i got some error on my code i cant figure it out, i have tried many step but its stay still, the data i call in provider is working but its still null in the page this is the code

this is my menu model

import 'package:kavarna/pages/models/gallery_model.dart';

class MenuModel {
  int? id;
  String? name;
  double? price;
  String? description;
  CategoryModel? category;
  DateTime? createdAt;
  DateTime? updatedAt;
  List<GalleryModel>? gallery;

  MenuModel({
    this.id,
    this.name,
    this.price,
    this.description,
    this.category,
    this.createdAt,
    this.updatedAt,
    this.gallery,
  });

  MenuModel.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
    price = double.parse(json['price'].toString());
    description = json['description'];
    category = CategoryModel.fromJson(json['category']);
    gallery = json['gallery']
        .map<GalleryModel>((gallery) => GalleryModel.formJson(json))
        .toList();
    createdAt = DateTime.parse(json['created_at']);
    updatedAt = DateTime.parse(json['updated_at']);
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'price': price,
      'description': description,
      'category': category?.toJson(),
      'gallery': gallery?.map((e) => e.toJson()).toList(),
      'created_at': createdAt,
      'updated_at': updatedAt,
    };
  }
}

and this is my provider

import 'package:kavarna/pages/models/menu_model.dart';
import 'package:kavarna/pages/services/menu_service.dart';

class MenuProvider with ChangeNotifier {
  List<MenuModel> _menuries = [];

  List<MenuModel> get menuries => _menuries;

  set menuries(List<MenuModel> menuries) {
    _menuries = menuries;
    notifyListeners();
  }

  Future<void> getMenus() async {
    try {
      List<MenuModel> menuries = await MenuService().getMenu();
      _menuries = menuries;
    } catch (e) {
      print(e);
    }
  }
}

idk wheres the problem is but its still shown error like the title i post, i try to comment the print(e); in my provider its gone but its still shown if im not the error is like on this photo

error result

for more the detail i got and error in the category_model is there a wrong code i wrote ?


class CategoryModel {
  int? id;
  String? name;

  CategoryModel({
    this.id,
    this.name,
  });

  CategoryModel.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
    };
  }
}

Upvotes: 1

Views: 5772

Answers (1)

esentis
esentis

Reputation: 4666

You are mapping the wrong variable here :

.map<GalleryModel>((gallery) => GalleryModel.formJson(json))

try it this way :

.map<GalleryModel>((gallery) => GalleryModel.formJson(gallery))

EDIT

Moreover, based on your response:

Response

I see no category field, however you are trying to map your category with json['category']. The only thing to category is categories_id and categories, maybe you meant json['categories'] ?

Upvotes: 3

Related Questions