The argument type 'Stream<dynamic>' can't be assigned to the parameter type 'Stream<QuerySnapshot>?'

2,934

Change it to this

CollectionReference posts = FirebaseFirestore.instance.collection('posts');
.
.
.
stream: posts.snapshots()
Share:
2,934
Admin
Author by

Admin

Updated on December 31, 2022

Comments

  • Admin
    Admin over 1 year

    I am getting an error "The argument type 'Stream' can't be assigned to the parameter type 'Stream?" while running this code:

    import 'package:app_using_database/post.dart';
    import 'package:flutter/material.dart';
    import 'package:firebase_auth/firebase_auth.dart';
    import 'package:cloud_firestore/cloud_firestore.dart';
    
    class Home extends StatelessWidget {
      Stream postStream =
          FirebaseFirestore.instance.collection('posts').snapshots();
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Container(
            padding: EdgeInsets.symmetric(horizontal: 10),
            child: SafeArea(
              child: StreamBuilder<QuerySnapshot>(
                stream: postStream,
                builder:
                    (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
                  if (snapshot.hasError) {
                    return Text('Something went wrong');
                  }
    
                  if (snapshot.connectionState == ConnectionState.waiting) {
                    return Text("Loading");
                  }
    
                  return new ListView(
                    children: snapshot.data!.docs.map((DocumentSnapshot document) {
                      Map<String, dynamic> data =
                          document.data() as Map<String, dynamic>;
                      return Post();
                    }).toList(),
                  );
                },
              ),
            ),
          ),
        );
      }
    }
    

    I am trying to get posts from firebase but this error appears. It also says

    • 'Stream' is from 'dart:async'.
    • 'QuerySnapshot' is from 'package:cloud_firestore/cloud_firestore.dart'
    • Burak Cabadan
      Burak Cabadan almost 3 years
      you should add ?to your Streamdefinition. Stream postStream = FirebaseFirestore.instance.collection('posts').snapshots(); to Stream? postStream = FirebaseFirestore.instance.collection('posts').snapshots(); to make it nullable
    • Admin
      Admin almost 3 years
      Done this. Nothing changes. It still gives the same error.