Home > OS >  how can i fix this error of my todo list? SOLVED
how can i fix this error of my todo list? SOLVED

Time:10-20

I have made an to do list with firebase. but when i click to create a new to do, i can't see anything apear on my page but in firebase it does show the string.

How can i fix this

(this is in flutter)

logcat: 2022-10-19 15:24:50.758 23369-23584 flutter com.example.voorbeeld I apen created

import 'package:flutter/material.dart';

import 'package:cloud_firestore/cloud_firestore.dart';



class video_info extends StatefulWidget {
  @override
  _video_infoState createState() => _video_infoState();
}

class _video_infoState extends State<video_info> {
  String todoTitle = "";

  createTodos() {
    DocumentReference documentReference =
        FirebaseFirestore.instance.collection("MyTodos").doc(todoTitle);

    //Map
    Map<String, String> todos = {"todoTitle": todoTitle};

    documentReference.set(todos).whenComplete(() {
      print("$todoTitle created");
    });
  }

  deleteTodos(item) {
    DocumentReference documentReference =
    FirebaseFirestore.instance.collection("MyTodos").doc(item);

    documentReference.delete().whenComplete(() {
      print("$item deleted");
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("mytodos"),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          showDialog(
              context: context,
              builder: (BuildContext context) {
                return AlertDialog(
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(8)),
                  title: Text("Add Todolist"),
                  content: TextField(
                    onChanged: (String value) {
                      todoTitle = value;
                    },
                  ),
                  actions: <Widget>[
                    TextButton(
                onPressed:() {
                  createTodos();
                  Navigator.of(context).pop();
                },
                child: Text("Add"))
                ],
                );
              });
        },
        child: Icon(
          Icons.add,
          color: Colors.white,
        ),
      ),
      body: StreamBuilder(
          stream: FirebaseFirestore.instance.collection("Mytodos").snapshots(),
          builder: (context, snapshots) {
            if (snapshots.hasData) {
              return ListView.builder(
                  shrinkWrap: true,
                  itemCount: snapshots.data?.docs.length,
                  itemBuilder: (context, index) {
                    DocumentSnapshot documentSnapshot =
                    snapshots.data!.docs[index];
                    return Dismissible(
                        onDismissed: (direction) {
                          deleteTodos(documentSnapshot["todoTitle"]);
                        },
                        key: Key(documentSnapshot["todoTitle"]),
                        child: Card(
                          elevation: 4,
                          margin: EdgeInsets.all(8),
                          shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(8)),
                          child: ListTile(
                            title: Text(documentSnapshot["todoTitle"]),
                            trailing: IconButton(
                                icon: Icon(
                                  Icons.delete,
                                  color: Colors.red,
                                ),
                                onPressed: () {
                                  deleteTodos(documentSnapshot["todoTitle"]);
                                }),
                          ),
                        ));
                  });
            } else {
              return Align(
                alignment: FractionalOffset.bottomCenter,
                child: CircularProgressIndicator(),
              );
            }
          }),
    );
  }}

also does anyone know a link to an tuturial where they explain how i can link the database to a user login.

CodePudding user response:

You're using another collection.

You are adding your todo to this collection:

FirebaseFirestore.instance.collection("MyTodos")

But in your StreamBuilder you use the collection "Mytodos":

stream: FirebaseFirestore.instance.collection("Mytodos").snapshots(),

CodePudding user response:

Try creating a stream variable on state class

  late final myStream  = FirebaseFirestore.instance.collection("MyTodos").snapshots();

  @override
  Widget build(BuildContext context) {
  
  ....
   body: StreamBuilder(
          stream: myStream
  • Related