initial data for StreamProvider<QuerySnapshot>.value flutter

Viewed 4219

what should be the initialData for StreamProvider.value
I'm unable to initialize it to null

 Widget build(BuildContext context) {
    return StreamProvider<QuerySnapshot>.value(
       value: DatabaseService.withoutUID().brews,
       initialData: null,
       ......

initialData:null is showing an error : The argument type 'Null' can't be assigned to the parameter type 'QuerySnapshot<Object?>'.


Below is the DatabaseService helper class

class DatabaseService{

  final String uid;

  DatabaseService(this.uid);
  DatabaseService.withoutUID():uid="" ;


  //collection reference
  final CollectionReference brewCollection = FirebaseFirestore.instance.collection('brews');

  Future updateUserData(String sugars, String name, int strength) async{
    return await brewCollection.doc(uid).set({
      "sugars": sugars,
      "name" : name,
      "strength" : strength,
    });
  }

  //get brews stream
  Stream<QuerySnapshot>? get brews{
    return brewCollection.snapshots();
  }

}


Here I'm calling that stream

class Home extends StatelessWidget {

final AuthService _auth = AuthService();

  @override
  Widget build(BuildContext context) {
    return StreamProvider<QuerySnapshot>.value(
      value: DatabaseService.withoutUID().brews,
      initialData: null,
      child: Scaffold(
        backgroundColor: Colors.brown[50],
        appBar: AppBar(
          backgroundColor: Colors.brown[400],
          title: Text("Brew Crew"),
          elevation: 0.0,
          actions: [
            ElevatedButton.icon(
              onPressed: () async {
                await _auth.signOut();
              }, .......

In the tutorial, I'm following they are using the flutter version where initialData is not compulsory in my case (flutter v 2.2.3) it is mandatory to provide initialData while we are using StreamProvider. So I wondering what value I should provide there.

BTW I'm following this tutorial from The net ninja channel in yt

5 Answers

I think you should replace QuerySnapshot with List<Brew>. and your initialData: [] and everything should work fine.

  class DatabaseService {
  final String uid;

  DatabaseService(this.uid);

  DatabaseService.withoutUID() : uid = "";

  //collection reference
  final CollectionReference brewCollection =
      FirebaseFirestore.instance.collection('brews');

  Future updateUserData(String sugars, String name, int strength) async {
    return await brewCollection.doc(uid).set({
      "sugars": sugars,
      "name": name,
      "strength": strength,
    });
  }

  //get brews stream
  Stream<Brew>? get brews {
    return brewCollection.snapshots();
  }

  List<Brew> _brewListFromSnapshot(QuerySnapshot snapshot) {
    return snapshot.documents.map((doc) {
      //print(doc.data);
      return Brew(
          name: doc.data['name'] ?? '',
          strength: doc.data['strength'] ?? 0,
          sugars: doc.data['sugars'] ?? '0');
    }).toList();
  }
}

your home.dart

class Home extends StatelessWidget {

final AuthService _auth = AuthService();

  @override
  Widget build(BuildContext context) {
    return StreamProvider<List<Brew>>.value(
      value: DatabaseService.withoutUID().brews,
      initialData: [],
      child: Scaffold(
        backgroundColor: Colors.brown[50],
        appBar: AppBar(
          backgroundColor: Colors.brown[400],
          title: Text("Brew Crew"),
          elevation: 0.0,
          actions: [
            ElevatedButton.icon(
              onPressed: () async {
                await _auth.signOut();
              }, .......

Instead of returning a "StreamProvider.value", you can simple return "StreamProvider<QuerySnapshot?>.value". Then the initial value will accept "null" values.

make QuerySnapshot nullable by adding ? after it :

return StreamProvider<QuerySnapshot?>.value(
  initialData: null, 
return StreamProvider<QuerySnapshot?>.value(
  initialData: [], 

The easiest way to solve this problem, is just to make the nullable by adding '?' like:-StreamProvider<QuerySnapshot?>.value

Related