How to make the ListView.builder for the coming data from firebase

Viewed 34

There are two different codes, one is my code and the other is the YouTuber code of which I am watching the video. First my code :

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:todo_app/screens/new_task.dart';
class MyHome extends StatefulWidget {
  const MyHome({Key? key}) : super(key: key);

  @override

_ myHomeState createState() => _myHomeState(); }

class _myHomeState extends State<MyHome> {
  String uid='';
  @override
  void initState() {
    getUid();
    super.initState();
  }
  getUid()async{
    FirebaseAuth auth = FirebaseAuth.instance;
    final User? user = await auth.currentUser;
    setState(() {
      uid = user!.uid;
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('TODO'),
        backgroundColor: Colors.purple,
      ),
      body: Container(
    height: MediaQuery.of(context).size.height ,
    width: MediaQuery.of(context).size.width,
    color: Colors.white10,
    child: StreamBuilder(stream: 
    FirebaseFirestore.instance.collection('tasks').doc(uid).collection('myTasks').snapshots(),
      builder: (context,snapshot) {
      if(snapshot.connectionState == ConnectionState.waiting){
       return Center(
         child:
         CircularProgressIndicator(),
       );

      }
      else{
       final docs = snapshot.data;

        return ListView.builder(itemCount: , itemBuilder: ( context,  index) {
          return Container(
            child:
            Column(
              children: [
               Text(docs [index] ['title'])
              ],
            ),
          );
        },);
      }
      },),
  ),
  floatingActionButton: FloatingActionButton(
    child: Icon(Icons.add,color: Colors.white,),
    backgroundColor: Theme.of(context).primaryColor,
    onPressed: (){
      Navigator.push(context, MaterialPageRoute(builder: (context)=>AddTask()));
    },
  ),
);

} } 2nd the Youtuber code:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:todo_app/screens/new_task.dart';
class MyHome extends StatefulWidget {
  const MyHome({Key? key}) : super(key: key);

  @override

_ myHomeState createState() => _myHomeState(); }

class _myHomeState extends State<MyHome> {
  String uid='';
  @override
  void initState() {
    getUid();
    super.initState();
  }
  getUid()async{
    FirebaseAuth auth = FirebaseAuth.instance;
    final User? user = await auth.currentUser;
    setState(() {
      uid = user!.uid;
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('TODO'),
        backgroundColor: Colors.purple,
      ),
      body: Container(
    height: MediaQuery.of(context).size.height ,
    width: MediaQuery.of(context).size.width,
    color: Colors.white10,
    child: StreamBuilder(stream: 
    FirebaseFirestore.instance.collection('tasks').doc(uid).collection('myTasks').snapshots(),
      builder: (context,snapshot) {
      if(snapshot.connectionState == ConnectionState.waiting){
       return Center(
         child:
         CircularProgressIndicator(),
       );

      }
      else{
       final docs = snapshot.data;

        return ListView.builder(itemCount: , itemBuilder: ( context,  index) {
          return Container(
            child:
            Column(
              children: [
               Text(docs [index] ['title'])
              ],
            ),
          );
        },);
      }
      },),
  ),
  floatingActionButton: FloatingActionButton(
    child: Icon(Icons.add,color: Colors.white,),
    backgroundColor: Theme.of(context).primaryColor,
    onPressed: (){
      Navigator.push(context, MaterialPageRoute(builder: (context)=>AddTask()));
    },
  ),
);
   }
   }

I have the issue in else{final docs = snapshot.data.documents;}this is Youtuber code, in my code there is not showing .documents; after snapshot.data. 2nd issue: I have the 2nd issue in item count: docs. length this is the YouTuber code, in my code, there is no showing .length after docs.. 3rd issue I am facing : I have the issue in Text(docs [index] ['title']) this is the Youtuber code, in my code: Text(docs! [index] ['title']) flutter was throwing an error 'please add the null check after docs', and I gave the null check, but after giving the null check flutter is throwing another error about [index], the error is :error: The operator '[]' isn't defined for the type 'Object'. The video I am watching is from ten months ago. I don't know the updated code. Please help me. I hope those who would be seeing my code, would be understanding my issue. link: https://www.youtube.com/watch?v=3kuvRjDS3yk&list=PL9n0l8rSshSnSO4dNTJmKGNa0VNHrCQFR&index=6

1 Answers

The youtuber you are following on with is using an outdated version of firestore and the video is probably more than a year old.

Would advise you to get comfortable with reading documentation for example firebase, as they explain that now it's docs instead of documents.

Change you code to this:

 final docs = snapshot.data.docs;

    return ListView.builder(itemCount: docs.length, itemBuilder: ( context,  index) 
Related