Reputation: 549
I have problems with mapping data from firestore into a model. I've been trying for hours and always getting cast errors (exceptions) like 'Map can't be cast to Map' or 'type '_InternalLinkedHashMap' is not a subtype of type 'Map'
I tried examples from these links:
How do you load array and object from Cloud Firestore in Flutter
https://github.com/dart-lang/sdk/issues/36836
and many more, still don't understand how this thing works.
Here is my entity and model class:
Entity
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'],
);
}
}
Model:
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'];
}
Upvotes: 1
Views: 2380
Reputation: 549
Answer:
in model, method 'fromEntity' I didn't cast entity map (dynamics) to model map (object) which is exactly where exception occurred. Every "value" from this dynamic map should have been transformed to object, which I was doing wrong all the time.
static ScreenHome fromEntity(ScreenHomeEntity entity) {
return ScreenHome(
entity.userName,
id: entity.id,
userEmail: entity.userEmail,
mainDoor: entity.mainDoor,
doors: entity.doors
.map((k, v) => MapEntry(k as String, ScreenHomeDoor.fromMap(v))),
);
}
for the whole time I was trying to do it with MapEntry(k as String, v as ScreenHomeDoor.fromMap(v))
A simple and stupid mistake....
Upvotes: 3