import 'package:cloud_firestore/cloud_firestore.dart';
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'];
}
@JsonSerializable()
class Food extends MyCard {
@override
String name;
@override
String desc;
@override
String image;
@override
String flav;
@override
double price;
Food(
{required this.name,
required this.desc,
required this.image,
required this.flav,
required this.price})
: super(name, desc, image, flav, price);
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,
);
static 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);
static Map<String, dynamic> toJson() => _$FoodToJson(this);
final FoodRef = FirebaseFirestore.instance.collection('listfood').withConverter<Food>(
fromFirestore: (snapshot, _) => Food.fromJson(snapshot.data()!),
toFirestore: (food, _) => Food.toJson(),
);
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',
),
];
}
误差直线误差_$FoodToJson(本);static Map<String, dynamic> toJson() => _$FoodToJson(this);
我自己也不知道我错在哪里。代码可以从数据库中获取数据吗?我是一个新手。因为有人说它可以从数据库中获取数据,比如**doc[index][“name”]**来替换foodlist中的name。
如果解决行静态Map〈String,dynamic〉toJson()=〉_$FoodToJson(this)的错误;可以像下面的代码那样使用Food.toJson().name吗?
static var foodList = [
Food(
name: Food.toJson().name,
desc: Food.toJson().desc,
price: Food.toJson().price
image: Food.toJson().image,
flav: Food.toJson().flav,
),
]
1条答案
按热度按时间vdzxcuhz1#
首先,您已经错误地配置了@JsonSerializable,我建议您完成这个video,它解释了如何使用build_runner配置@JsonSerializable。每次在模型文件中进行更改时,请运行
flutter pub run build_runner build
。我在设置中重新创建了这个问题,发现要在继承类上使用@JsonSerializable(),必须使用@JsonSerializable(explicitToJson:true)从类似线程引用this answer。
我已经更新了你的代码,并张贴在这个要点:models.dart。运行
flutter pub run build_runner build
,然后将此文件导入到您想要的位置,并使用firebase和Convertor,如下所示:您可能需要根据自己的要求对上述内容进行更改。有关更多信息,请参阅官方flutterfire文档,对于@JsonSerializable,请浏览此thread