Home > Mobile >  I cannot get the data from the firestore into my application
I cannot get the data from the firestore into my application

Time:10-23

I am new to programming and while i was trying to create a small TODO app, i was able to save the data into cloud firestore but the problems ocurred when i tried to retrieve the data using Streambuilder. I was following old tutorials before null safety , so i suspect that issues are regarding the null safety. The code works without any errors in android studio but the data from firestore can't be retrieved. The code is as follows:

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

class AddTODOlist extends StatelessWidget {
  static String id = 'add_todolist';

  final TextEditingController _controller = TextEditingController();
  void _addUser(){
    FirebaseFirestore.instance.collection("Todos").add({"title" : _controller.text});
    _controller.text = "";
  }

  Widget _buildList(QuerySnapshot? snapshot){
    return ListView.builder(
      itemCount: snapshot!.docs.length,
      itemBuilder: (context, index){
        final doc = snapshot.docs[index];
        final map = (doc.data()as dynamic)['title'];
        return ListTile(
          title: Text(map,style: TextStyle(color: Colors.black),),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Loan System',),
        centerTitle: true,
      ),
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            children: [
              Row(
                children: [
                  Expanded(
                    child: TextField(
                      controller: _controller,
                      decoration: InputDecoration(
                        hintText: 'Add new user',
                      ),
                    ),
                  ),
                  TextButton(
                    style: ButtonStyle(
                      backgroundColor: MaterialStateProperty.all<Color>(Colors.blue),
                    ),
                      onPressed:(){
                      _addUser();
                      },
                      child: Text('Add',
                      style: TextStyle(
                        color: Colors.white,
                      ),),
                  ),
                ],
              ),
             StreamBuilder<QuerySnapshot?>(
             stream: FirebaseFirestore.instance.collection('todos').snapshots(),
             builder: (context, snapshot){
               if(!snapshot.hasData) return LinearProgressIndicator();
               else {
                 return Expanded(
                 child: _buildList(snapshot.data),
               );
               }
             }
             ),
            ],
          ),
        ),
      ),
    );
  }
}

CodePudding user response:

You have an error in the spelling of the collection name. Remember that Firestore is case sensitive

Todos vs todos

FirebaseFirestore.instance.collection("Todos").add({"title" : _controller.text});

and here:

stream: FirebaseFirestore.instance.collection('todos').snapshots(),

Let me know if this does not help

  • Related