【发布时间】:2020-05-05 05:23:45
【问题描述】:
我的 Firebase 数据库模型如下所示:
dbRef
|___userId
|____Cars
|____Alfa_Romeo
| |____Year: 1992
| |____Price: 10000
|__________Audi
| |____Year: 1998
| |____Price: 3000
|___________BMW
|____Year: 2001
|____Price: 7000
然后我有一辆看起来像的Class Car
import 'package:json_annotation/json_annotation.dart';
part 'car.g.dart';
/// An annotation for the code generator to know that this class needs the
/// JSON serialization logic to be generated.
@JsonSerializable()
class Car {
Carta(this.model, this.details);
String model;
List details;
factory Car.fromJson(Map<String, dynamic> json) => _$CarFromJson(json);
Map<String, dynamic> toJson() => _$CarToJson(this);
}
现在,当我将数据设置到 Firebase 时,我得到了这个:
dbRef
|___userId
|____Cars
|____Alfa_Romeo
|____Year: 1992
|____Price: 10000
...我希望它像第一张图中的模型。 如何将详细信息嵌套在“模型”子项中? 有人可以帮我解决这个问题吗?
编辑:只是为了确保我清楚我想要什么。 此代码来自 Flutter 团队docs example:
import 'address.dart';
import 'package:json_annotation/json_annotation.dart';
part 'user.g.dart';
@JsonSerializable(explicitToJson: true)
class User {
String firstName;
Address address;
User(this.firstName, this.address);
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
Map<String, dynamic> toJson() => _$UserToJson(this);
}
此代码生成以下输出:
name: John, address: {street: My st., city: New York}}
我想要实现的是 John 成为一个 Child 键和 Adress 嵌套在里面,因为可能有多个 Adress Array。 Adress 字段应该成为一个 Key(它将是唯一的),然后它将有一个项目映射,每个项目只有 2 个字段描述和价格。
【问题讨论】:
标签: firebase flutter firebase-realtime-database dart