trying to use streambuilder to get data from firestore but it gives me an error

Viewed 26

The error it brings on the screen after running the flutter run command is:

NoSuchMethodError:'documents'method not found Receiver:Instance of '_JsonQuerySnapshot' Arguments:[]

Below is my code

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

import 'Navigation.dart';


class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({Key key}) : super(key: key);

  @override
  State<MyStatefulWidget> createState() => Forums();
}

class Forums extends State<MyStatefulWidget> {
  int currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
        actions: [
          IconButton(
            icon: const Icon(Icons.notifications_none),
            onPressed: () {},
          ),
          IconButton(
            icon: const Icon(Icons.menu_book),
            onPressed: () {},
          )
        ],
        backgroundColor: Colors.greenAccent,
        shape: const RoundedRectangleBorder(
            borderRadius: BorderRadius.only(
                bottomLeft: Radius.circular(1000),
                bottomRight: Radius.circular(2000))),
        bottom: const PreferredSize(
          preferredSize: Size.fromHeight(200),
          child: SizedBox(),
        ),
      ),
      drawer: NavigationDrawer(),
      body:       
      StreamBuilder(
        stream: FirebaseFirestore.instance.collection("courses").snapshots(),
        builder:(context, snapshot) {
          return ListView.builder(
            itemCount: snapshot.data.documents.length,
            itemBuilder: (context, index) {
              DocumentSnapshot course = snapshot.data.documents[index];
              return ListTile(
                leading: Image.network(course['img']),
                title: Text(course['name']),
              );
            },
          );
        },
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        backgroundColor: const Color.fromARGB(255, 247, 247, 247),
        selectedItemColor: Colors.green,
        unselectedItemColor: Colors.greenAccent,
        iconSize: 30,
        showSelectedLabels: false,
        showUnselectedLabels: false,
        currentIndex: currentIndex,
        onTap: (index) => setState(() => currentIndex = index),
        items: const [
          BottomNavigationBarItem(
            icon: Icon(
              Icons.home,
              color: Colors.greenAccent,
            ),
            label: "Home",
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.group, color: Colors.greenAccent),
            label: "Forums",
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.school, color: Colors.greenAccent),
            label: "Courses",
          ),
          BottomNavigationBarItem(
              icon: Icon(Icons.person, color: Colors.greenAccent),
              label: "Guide"),
        ],
      ),
    );

  }
}

1 Answers

snapshot can be null. So put a null check by using hasData.

if (snapshot.hasData) {
  return ListView.builder(...);
}
return SomeLoader();
Related