簡體   English   中英

“Null”類型不是“Map”類型的子類型<string, dynamic> '</string,>

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

嗨,我的代碼出現了一些錯誤,我無法弄清楚,我已經嘗試了很多步驟,但它仍然保持不動,我在提供程序中調用的數據正在工作,但頁面中仍然是 null 這是代碼

這是我的菜單 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,
    };
  }
}

這是我的提供者

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問題出在哪里,但仍然顯示錯誤,例如我發布的標題,我嘗試評論打印(e); 在我的提供商中它消失了,但如果我不是這張照片上的錯誤,它仍然會顯示

錯誤結果

您在這里映射錯誤的變量:

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

試試這種方式:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM