Flutter 颤振返回解析来自服务的动态对象
我创建了多个模型,并使用json_序列化 例如公司和员工等Flutter 颤振返回解析来自服务的动态对象,flutter,dart,Flutter,Dart,我创建了多个模型,并使用json_序列化 例如公司和员工等 import 'package:json_annotation/json_annotation.dart'; part 'company.g.dart'; @JsonSerializable() class Company { Company({this.id, this.name}); String id; String name; factory Company.fromJson(Map<String
import 'package:json_annotation/json_annotation.dart';
part 'company.g.dart';
@JsonSerializable()
class Company {
Company({this.id, this.name});
String id;
String name;
factory Company.fromJson(Map<String, dynamic> json) => _$CompanyFromJson(json);
Map<String, dynamic> toJson() => _$CompanyToJson(this);
}
import'package:json_annotation/json_annotation.dart';
“公司g.dart”部分;
@JsonSerializable()
阶级公司{
公司({this.id,this.name});
字符串id;
字符串名;
factory Company.fromJson(Map json)=>$CompanyFromJson(json);
映射到JSON()=>$CompanyToJson(这个);
}
import'package:json_annotation/json_annotation.dart';
“雇员g.dart”部分;
@JsonSerializable()
班级员工{
雇员({this.id,this.name,this.email,this.phone,this.photo});
字符串id;
字符串名;
字符串电子邮件;
字符串电话;
弦乐照片;
factory Employee.fromJson(Map json)=>$EmployeeFromJson(json);
Map to JSON()=>$EmployeeToJson(这个);
}
现在我想创建一个可重用的服务,它可以返回类型为Company、Employee或其他类型的流。
我使用的是Firebase,所以返回类型是Map
示例服务类
class BaseService<T> {
final String collection;
CollectionReference _collectionRef;
FirebaseBase({@required this.collection}) {
_collectionRef = Firestore.instance.collection(collection);
}
Stream<List<T>> find() {
return inColRef.snapshots().map((list) {
return list.documents.map((doc) {
final Map<String, dynamic> data = doc.data;
data['id'] = doc.documentID;
return data;
}).toList();
});
}
}
类基本服务{
最终字符串集合;
CollectionReference\u collectionRef;
FirebaseBase({@required this.collection}){
_collectionRef=Firestore.instance.collection(集合);
}
流查找(){
返回inColRef.snapshots().map((列表){
退货清单.单据.地图((单据){
最终地图数据=单据数据;
数据['id']=doc.documentID;
返回数据;
}).toList();
});
}
}
如何将退货数据(Map)转换为公司或员工类型
这些类可以使用fromJson(数据)的工厂。
但是我不能返回t.fromJson(数据)
我想去
Stream<List<Company>> companies = ServiceBase('companies').find();
Stream<List<Employee>> employees = ServiceBase('employee').find();
Stream companys=ServiceBase('companys').find();
Stream employees=ServiceBase('employeer')。find();
这是您使用当前代码完成一般分配的方式:
class BaseService<T> {
final String collection;
CollectionReference _collectionRef;
FirebaseBase({@required this.collection}) {
_collectionRef = Firestore.instance.collection(collection);
}
Stream<List<T>> find() {
return inColRef.snapshots().map((list) {
return list.documents.map((doc) {
final Map<String, dynamic> data = doc.data;
data['id'] = doc.documentID;
return _build(data) as T;
}).toList();
});
}
dynamic _build(final Map<String, dynamic> data) {
if(collection == 'companies') {
return Company.fromJson(data);
} else if(collection == 'employee') {
return Employee.fromJson(data);
}
... throw if invalid collection passed ? ...
}
}
类基本服务{
最终字符串集合;
CollectionReference\u collectionRef;
FirebaseBase({@required this.collection}){
_collectionRef=Firestore.instance.collection(集合);
}
流查找(){
返回inColRef.snapshots().map((列表){
退货清单.单据.地图((单据){
最终地图数据=单据数据;
数据['id']=doc.documentID;
将构建(数据)返回为T;
}).toList();
});
}
动态构建(最终地图数据){
如果(集合=‘公司’){
返回Company.fromJson(数据);
}else if(集合=‘员工’){
返回Employee.fromJson(数据);
}
…如果传递了无效集合,则引发。。。
}
}
这将被称为:
Stream<List<Company>> companies = BaseService<Company>('companies').find();
Stream<List<Employee>> employees = BaseService<Employee>('employee').find();
Stream companys=BaseService('companys').find();
Stream employees=BaseService('employeer')。find();
我建议您使用模板方法模式使用不同的对象结构,例如:
class CompanyService extends BaseService<Company> {
CompanyService() : super('companies');
Company build(final Map<String, dynamic> data) => Company.fromJson(data);
}
class EmployeeService extends BaseService<Employee> {
EmployeeService() : super('employee');
Employee build(final Map<String, dynamic> data) => Employee.fromJson(data);
}
abstract class BaseService<T> {
final String collection;
CollectionReference _collectionRef;
BaseService(this.collection) {
_collectionRef = Firestore.instance.collection(collection);
}
T build(final Map<String, dynamic> data);
Stream<List<T>> find() {
return inColRef.snapshots().map((list) {
return list.documents.map((doc) {
final Map<String, dynamic> data = doc.data;
data['id'] = doc.documentID;
return build(data);
}).toList();
});
}
}
class CompanyService扩展了BaseService{
CompanyService():超级(“公司”);
公司构建(最终地图数据)=>Company.fromJson(数据);
}
类EmployeeService扩展了BaseService{
EmployeeService():超级(“员工”);
员工构建(最终地图数据)=>Employee.fromJson(数据);
}
抽象类基类服务{
最终字符串集合;
CollectionReference\u collectionRef;
BaseService(this.collection){
_collectionRef=Firestore.instance.collection(集合);
}
T构建(最终地图数据);
流查找(){
返回inColRef.snapshots().map((列表){
退货清单.单据.地图((单据){
最终地图数据=单据数据;
数据['id']=doc.documentID;
返回构建(数据);
}).toList();
});
}
}
这将导致调用代码如下所示:
Stream<List<Company>> companies = CompanyService().find();
Stream<List<Employee>> employees = EmployeeService().find();
Stream companies=CompanyService().find();
Stream employees=EmployeeService().find();
谢谢,布莱恩。您推荐的解决方案很有帮助。我学到了一些新东西。
Stream<List<Company>> companies = CompanyService().find();
Stream<List<Employee>> employees = EmployeeService().find();