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

Viewed 3426

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'
1 Answers

Change it to this

CollectionReference posts = FirebaseFirestore.instance.collection('posts');
.
.
.
stream: posts.snapshots()
Related