0

when I want to create

"final Stream _product = FirebaseFirestore.instance.collection("product").snapshots(); "

for getting data from firebase but I get this Error

this is mycode

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:store/widgets/custonActionBar.dart';

class MainHome extends StatelessWidget {
  // const MainHome({super.key});

  final Stream<QuerySnapshot> _product =
      FirebaseFirestore.instance.collection("product").snapshots();

  @override
  Widget build(BuildContext context) {
    return Container(
        child: Stack(
      children: [
        StreamBuilder(
          stream: _product,
          builder:
              (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
            //if the connection has Error
            if (snapshot.hasError) {
              return Center(
                child: Text('Error ${snapshot.error}'),
              );
            }
            //if connection was ok = 200 ok
            if (snapshot.connectionState == ConnectionState.waiting) {
              return Center(
                child: CircularProgressIndicator(),
              );
            }
            return Container(
              child: Center(child: Text('productName : shoes')),
            );
          },
        ),
        custonActioBar("Home", "0")
      ],
    ));
  }
}

I try to use futureBuilder but it did'nt work

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
ali ma
  • 5
  • 2
  • 2
    Did you initialize the app? It has nothing to do with the builder. You need to setup your Firebase with the correct credential files for your app – casraf Nov 20 '22 at 08:50

1 Answers1

1

If you correctly connect your app with firebase with all the credentials try these steps.

Step 1

Add firebase_core in your project's pubspac.ymal

Step 2

Go to your main.dart file and make your main function async

void main() async {
// add this line of code
  WidgetsFlutterBinding.ensureInitialized();

// wait for firebase to initializ
  await Firebase.initializeApp();
  runApp(MyApp());
}

Step 3

define your firebase instance in separate variable

  var fireStore = FirebaseFirestore.instance;
  final Stream<QuerySnapshot> _product= 
       fireStore.collection("product").snapshots();
Mashood .H
  • 926
  • 6
  • 16