Home > database >  InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'DocumentSnapshot&l
InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'DocumentSnapshot&l

Time:04-10

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):

Firestore structure

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>>

CodePudding user response:

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()!);

  • Related