class Product {
String status;
List<List> note;
List<List> table;
Product({this.status, this.note, this.table});
Product.fromJson(Map<String, dynamic> json) {
status = json['status'];
if (json['note'] != null) {
note = <List>[];
json['note'].forEach((v) { note.add(new List.fromJson(v)); });
}
if (json['table'] != null) {
table = <List>[];
json['table'].forEach((v) { table.add(new List.fromJson(v)); });
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['status'] = this.status;
if (this.note != null) {
data['note'] = this.note.map((v) => v.toJson()).toList();
}
if (this.table != null) {
data['table'] = this.table.map((v) => v.toJson()).toList();
}
return data;
}
}
The class 'List' doesn't have a constructor named 'fromJson'. Try invoking a different constructor, or define a constructor named 'fromJson'. / Error in List.fromJson and v.toJson
CodePudding user response:
The error message says it pretty straightforward.
you can not call
new List.fromJson(v)
Try to create a dart Iterable from your json and use .from() constructor
List<E>.from(
Iterable elements,
{bool growable = true}
)
Or try other constructors defined by the List Class https://api.dart.dev/stable/2.14.2/dart-core/List-class.html
This lib might also be helpful https://pub.dev/packages/json_serializable
CodePudding user response:
The list is an abstract class. It has not a constructor fromJson
. If you want to convert from JSON into list, you must assign a list with Elements (Note
) Like:
//Generic form
List<E>
// For note
List<Note> note;
// call like that
json['note'].forEach((v) { note.add(new Note.fromJson(v)); });
Model class of Note:
class Note {
String title;
String description;
Note({this.title, this.description});
Note.fromJson(Map<String, dynamic> json) {
title = json['title'];
description = json['description'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['title'] = this.title;
data['description'] = this.description;
return data;
}
}