Instance members can't be accessed from a factory constructor. Try removing the reference to the instance member.
How do you fix it? How should I fix it? Can anyone tell me that I have followed everything but it didn't work?
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',
),
];
}
I'm new to JSON and serialization. So I don't know if it's correct or not. Need advice too which if there is any mistake, please help me
CodePudding user response:
You need to make _$FoodFromJson
static like this:
static 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,
);