我在将数据从 firestore 映射到模型时遇到问题。我已经尝试了几个小时,并且总是遇到诸如“地图无法转换为地图”或“类型'_InternalLinkedHashMap'不是'地图'类型的子类型之类的转换错误(异常)
我尝试了这些链接中的示例:
如何在 Flutter 中从 Cloud Firestore 加载数组和对象
https://github.com/dart-lang/sdk/issues/36836
还有更多,仍然不明白这东西是如何工作的。
这是我的实体和模型类:
实体
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:equatable/equatable.dart';
class ScreenHomeEntity extends Equatable {
final String id;
final String userName;
final String userEmail;
final String mainDoor;
final Map<dynamic, dynamic> doors;
const ScreenHomeEntity(
this.id, this.userName, this.userEmail, this.mainDoor, this.doors);
@override
List<Object> get props => [id, userName, userEmail, mainDoor, doors];
@override
String toString() =>
'ScreenHomeEntity {id: $id, userName: $userName, userEmail: $userEmail, mainDoor: $mainDoor, doors: $doors}';
Map<String, Object> toJson() {
return {
'id': id,
'userName': userName,
'userEmail': userEmail,
'mainDoor': mainDoor,
'doors': doors,
};
}
static ScreenHomeEntity fromJson(Map<String, Object> json) {
return ScreenHomeEntity(
json['id'] as String,
json['userName'] as String,
json['userEmail'] as String,
json['mainDoor'] as String,
json['doors'] as Map<dynamic, dynamic>,
);
}
Map<String, Object> toDocument() {
return {
'userName': userName,
'userEmail': userEmail,
'mainDoor': mainDoor,
'doors': doors,
};
}
static ScreenHomeEntity fromDocument(DocumentSnapshot doc) {
return ScreenHomeEntity(
doc.documentID,
doc.data['userName'],
doc.data['userEmail'],
doc.data['mainDoor'],
doc.data['doors'],
);
}
}
模型:
import 'package:brava_flutter/data/entities/screen_home_entity.dart';
import 'package:meta/meta.dart';
@immutable
class ScreenHome {
final String id;
final String userName;
final String userEmail;
final String mainDoor;
final Map<String, ScreenHomeDoor> doors;
ScreenHome(this.userName,
{String userEmail = '',
String id,
String mainDoor,
Map<String, ScreenHomeDoor> doors})
: this.userEmail = userEmail ?? '',
this.id = id,
this.mainDoor = mainDoor ?? '',
this.doors = doors ?? Map<String, ScreenHomeDoor>();
ScreenHome copyWith({
String id,
String userName,
String userEmail,
String mainDoor,
Map<String, ScreenHomeDoor> doors,
}) {
return ScreenHome(
userName ?? this.userName,
id: id ?? this.id,
userEmail: userEmail ?? this.userEmail,
mainDoor: mainDoor ?? this.mainDoor,
doors: doors ?? this.doors,
);
}
@override
int get hashCode =>
id.hashCode ^
userName.hashCode ^
userEmail.hashCode ^
mainDoor.hashCode ^
doors.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is ScreenHome &&
runtimeType == other.runtimeType &&
id == other.id &&
userName == other.userName &&
userEmail == other.userEmail &&
mainDoor == other.mainDoor &&
doors == other.doors;
@override
String toString() =>
'ScreenHomeModel {id: $id, userName: $userName, userEmail: $userEmail, mainDoor: $mainDoor, doors: $doors}';
ScreenHomeEntity toEntity() =>
ScreenHomeEntity(id, userName, userEmail, mainDoor, doors);
static ScreenHome fromEntity(ScreenHomeEntity entity) {
return ScreenHome(
entity.userName,
id: entity.id,
userEmail: entity.userEmail,
mainDoor: entity.mainDoor,
doors: entity.doors, // EXCEPTION
);
}
}
class ScreenHomeDoor {
String name;
String address;
ScreenHomeDoor.fromMap(Map<dynamic, dynamic> data)
: name = data['name'],
address = data['address'];
}
