Reputation: 185
I'm having issues getting a stream of a document. When casting documentSnapshot.data() as DocumentSnapshot<Map<String, dynamic>>
I get the error:
_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'DocumentSnapshot<Map<String, dynamic>>' in type cast
I'm using bloc; the StreamSubscription is created in the bloc like this:
on<FetchContacts>((event, emit) async {
try {
emit(FetchingContacts().copyWith(
username: state.username,
status: Formz.validate([state.username])));
subscription = userDataRepository.getContacts().listen((contacts) =>
{print('adding $contacts'), add(ReceiveContacts(contacts))});
And the contacts stream is created in the repository like this (truncated):
Stream<List<Contact>> getContacts() {
final contactsStream = userRef.snapshots().transform(StreamTransformer<
DocumentSnapshot<Map<String, dynamic>>, List<Contact>>.fromHandlers(
handleData: (documentSnapshot, sink) =>
mapDocumentToContact(usersRef, userRef, documentSnapshot, sink)));
}
Future<void> mapDocumentToContact(etc)
final data =
documentSnapshot.data()! as DocumentSnapshot<Map<String, dynamic>>;
The error is thrown at final data = ...
This is the structure of the Firestore db (the document I'm trying to subscribe to is a user):
Any help is appreciated, I've seen questions being asked about a similar error that were resolved using Map<String, dynamic>>.from(snapshot.data())
instead of as Map<String, dynamic>>
but unfortunately I can't do this with DocumentSnapshot<Map<String, dynamic>>
Upvotes: 0
Views: 496
Reputation: 185
Figured it out! Turns out I don't have to use DocumentSnapshot<Map<String, dynamic>>
, at what works is in fact (similarly to the other questions I found): final data = Map<String, dynamic>.from(documentSnapshot.data()!);
Upvotes: 1