ERROR: type 'List<dynamic>' is not a subtype of type 'Uint8List?' in Flutter

Viewed 44

(Edit)

I saved the image data of the Uint8List type in the post model and saved at my local storage. And use fromJson() to import saved data back to the post model. However, the following error occurs in the thumbimage = json[POST_THUMBIMAGE];

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Uint8List?'

Why do these errors occur and how can it be solved?

My model class is as follows

class PostModel {
  late String category;
  late String title;
  Uint8List? thumbImage;


  PostModel({
    required this.category, 
    required this.title,
    this.thumbImage,
  });

  // error!
  PostModel.fromJson(Map<String, dynamic> json) {
    category = json[POST_CATEGORY] ?? "";
    title = json[POST_TITLE] ?? "";
    thumbImage = json[POST_THUMBIMAGE];
  }

  Map<String, dynamic> toJson() {
    var map = <String, dynamic>{};
    map[POST_CATEGORY] = category;
    map[POST_TITLE] = title;
    map[POST_THUMBIMAGE] = thumbImage;

    return map;
  }
}
1 Answers

Your image list as a List < Uint8List >?. And you try to put a List < Dynamic> inside then.

change the image list to Dynamic.

List<dynamic>? images;
Related