Flutter 使用firestore db实现颤振数据序列化和反序列化
我在《颤栗》中遇到了这个奇怪的错误。我使用的是自定义数据模型:Flutter 使用firestore db实现颤振数据序列化和反序列化,flutter,dart,google-cloud-firestore,Flutter,Dart,Google Cloud Firestore,我在《颤栗》中遇到了这个奇怪的错误。我使用的是自定义数据模型: class ScanData { final String userID; final String companyID; final String scanID; final String deviceID; final String model; final Map result; final DateTime dateTime; final GeoPoint geoPoint; ScanD
class ScanData {
final String userID;
final String companyID;
final String scanID;
final String deviceID;
final String model;
final Map result;
final DateTime dateTime;
final GeoPoint geoPoint;
ScanData(
{this.userID,
this.companyID,
this.scanID,
this.deviceID,
this.model,
this.result,
this.geoPoint,
this.dateTime});
factory ScanData.fromMap(Map data) {
return ScanData(
userID: data['userID'] ?? '',
companyID: data['companyID'] ?? '',
scanID: data['scanID'] ?? '',
deviceID: data['deviceID'] ?? '',
model: data['model'] ?? '',
result: data['result'] ?? {},
dateTime: data['dateTime'] as DateTime ?? DateTime.now(),
geoPoint: data['geoPoint'] as GeoPoint ?? ['77', '28'],
);
}
}
在集合类中创建了从firestore检索数据的方法:
class Collection<T> {
final FirebaseFirestore _db = FirebaseFirestore.instance;
final String path;
CollectionReference ref;
Collection({this.path}) {
ref = _db.collection(path);
}
Future<List<T>> getData() async {
var snapshots = await ref.get();
return snapshots.docs
.map((doc) => Global.models[T](doc.data) as T)
.toList();
}
}
错误(发生在全局fromMap中):
发生异常。
_TypeError(类型“()=>Map”不是类型“Map”的子类型)
我尝试在Scanda模型中加入以下内容
factory ScanData.fromMap(Map<String,dynamic> data) {...}
factory Scanda.fromMap(映射数据){…}
但这也带来了另一个错误:
Exception has occurred.
_TypeError (type '() => Map<String, dynamic>' is not a subtype of type 'Map<String, dynamic>')
发生异常。
_TypeError(类型“()=>Map”不是类型“Map”的子类型)
:|非常感谢任何提示。在Collection类的getData()方法中,
doc.data
不是属性,它是一个方法,所以只需添加()
我认为您需要从到JSON进行编码和解码。firestore里有个虫子。不确定这些方法是否有效。非常感谢,m8!
Exception has occurred.
_TypeError (type '() => Map<String, dynamic>' is not a subtype of type 'Map<dynamic, dynamic>')
factory ScanData.fromMap(Map<String,dynamic> data) {...}
Exception has occurred.
_TypeError (type '() => Map<String, dynamic>' is not a subtype of type 'Map<String, dynamic>')