你需要添加这个关键字
Services.fromJson(Map<String, dynamic> json) {
this.id = json['id'];
this.quantity = json['quantity'];
this.name = json['name'];
this.price = json['price'];
}
print(_billModel.services);
Iterable<dynamic> l = json.decode(_billModel.services.trim());
var services = l.map((value) => Services.fromJson(value)).toList();
你想编码jsonstring然后使用下面的方法
Map<String, dynamic> user = jsonDecode(jsonString);
print('Howdy, ${user['name']}!');
print('We sent the verification link to ${user['email']}.');
对于内部模型类
class User {
final String name;
final String email;
User(this.name, this.email);
User.fromJson(Map<String, dynamic> json)
: name = json['name'],
email = json['email'];
Map<String, dynamic> toJson() =>
{
'name': name,
'email': email,
};
}
- User.fromJson() 构造函数,用于构造新的 User 实例
来自地图结构。
- toJson() 方法,将 User 实例转换为地图。
解码逻辑的职责现在移到了模型内部
本身。使用这种新方法,您可以轻松解码用户。
Map userMap = jsonDecode(jsonString);
var user = User.fromJson(userMap);
print('Howdy, ${user.name}!');
print('We sent the verification link to ${user.email}.');
要对用户进行编码,请将 User 对象传递给 jsonEncode() 函数。
你不需要调用 toJson() 方法,因为 jsonEncode() 已经
为你做。
String json = jsonEncode(user);
欲了解更多信息,请阅读来自flutter.io的这篇文章