无法从工厂构造函数访问示例成员。请尝试移除对示例成员的引用。
**你如何解决它?**我应该如何解决它?谁能告诉我,我已经遵循了一切,但它没有工作?
import 'package:json_annotation/json_annotation.dart';
@JsonSerializable()
class MyCard {
String name;
String desc;
String image;
String flav;
double price;
MyCard(this.name, this.desc, this.image, this.flav, this.price);
factory MyCard.Food(name, desc, image, flav, price) =>
MyCard(name, desc, image, flav, price);
MyCard.fromJson(Map<String, dynamic> json)
: name = json['name'],
desc = json['desc'],
image = json['image'],
flav = json['flav'],
price = json['price'];
}
class Food extends MyCard {
@override
String name;
@override
String desc;
@override
String image;
@override
String flav;
@override
double price;
@JsonSerializable()
Food(
{required this.name,
required this.desc,
required this.image,
required this.flav,
required this.price})
: super(name, desc, image, flav, price);
Food _$FoodFromJson(Map<String, dynamic> json) => Food(
name: json['name']as String,
desc: json['desc']as String,
image: json['image']as String,
flav: json['flav']as String,
price: json['price']as double,
);
Map<String, dynamic> _$FoodToJson(Food instance) => <String, dynamic>{
'name': instance.name,
'desc': instance.desc,
'image': instance.image,
'flav': instance.flav,
'price': instance.price,
};
factory Food.fromJson(Map<String, dynamic> json)=> _$FoodFromJson(json);
Map<String, dynamic> toJson() => _$FoodToJson(this);
/*{
'name': name,
'desc': desc,
'image': image,
'flav': flav,
'price': price
};*/
static var foodList = [
Food(
name:'Fraise Cream',
desc:
'We have been loading up on the stone fruit and berries at the market. We have no self control to these summer gems. We have gross we can out of hand, our Strawberry...',
price: 2.50,
image: 'assets/images/food1.png',
flav: 'Strawberry Flovour Sweet Ice Cream',
),
Food(
name: 'Mandarine',
desc:
'We have been loading up on the stone fruit and berries at the market. We have no self control to these summer gems. We have gross we can out of hand, our Strawberry...',
price: 3.50,
image: 'assets/images/food2.png',
flav: 'Caramel Flovour Sweet Ice Cream',
)
];
}
class Beverage extends MyCard {
@override
String name;
@override
String desc;
@override
String image;
@override
String flav;
@override
double price;
Beverage(
{required this.name,
required this.desc,
required this.image,
required this.flav,
required this.price})
: super(name, desc, image, flav, price);
static var beverageList = [
Beverage(
name: 'test',
desc: 'We have test',
image: 'assets/images/food1.png',
price: 3.0,
flav: 'Strawberry test',
),
];
}
我是新的JSON和序列化。所以我不知道它是否正确或不。也需要建议,如果有任何错误,请帮助我
1条答案
按热度按时间bpzcxfmw1#
你需要像这样使
_$FoodFromJson
成为静态的: