Search code examples
fluttergoogle-cloud-firestoreflutter-streambuilder

Making the streambuilder stream: a variable breaks stream builder


I am trying to make a search bar that can query my firestore DB and return results.

I have a stream builder that looks like this:

StreamBuilder(
        //query to firestore db
        stream:  streamQuery,
        //builds widget for loading and compleation
        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {...

Which throws this error:

Couldn't infer type parameter 'T'.

Tried to infer 'dynamic' for 'T' which doesn't work:
  Parameter 'builder' declared as     'Widget Function(BuildContext, AsyncSnapshot<T>)'
                      but argument is 'Widget Function(BuildContext, AsyncSnapshot<QuerySnapshot<Object?>>)'.
The type 'dynamic' was inferred from:
  Parameter 'stream' declared as     'Stream<T>?'
                     but argument is 'Stream<dynamic>'.

Consider passing explicit type argument(s) to the generic.

What is odd is that if I replace the stream: with this:

db
        .collection('GearLockerItems')
        .where('itemName', isGreaterThanOrEqualTo: searchKey)
        .where('itemName', isLessThan: '${searchKey}z')
        .where('communityShare', isEqualTo: true)
        .where('reviewed', isEqualTo: true)
        .snapshots();

then it works. Having that firestore stream saved to a variable breaks things. FOr reference I am trying to follow this Stack Overflow questions:

https://stackoverflow.com/a/60873879/522607


Solution

  • I had to Add StreamBuilder<dynamic> to the Stream Builder:

    body: StreamBuilder<dynamic>(
            stream: streamQuery,
            builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {...