skip to Main Content

I want to fetch data from Firebase and update the value in a Text widget in Flutter. Additionally, I would like the Text widget to be automatically updated whenever the value changes

I am a newbie to flutter and I have no idea about this thing. please assist me with this…

2

Answers


  1. to create data in firestore!
    first create a button on place the method in it.

        createData() {
          FirebaseFirestore.instance
              .collection('your collection named')
              .doc("your document id or blank it")
              .set({
            "name": "Jackson",
            "age": 15,
          });
        }
    

    for fetching data you have to create a widget that returns Text;

        StreamBuilder(
                stream: FirebaseFirestore.instance
                    .collection('your collection named')
                    .doc("your document id or blank it")
                    .snapshots(),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    DocumentSnapshot userData = snapshot.data!;
                    return Text(userData["name"]);
                  } else if (snapshot.hasError) {
                    print(Error); //just for checking
                  }
                  return const CircularProgressIndicator(); // loading progress indicate
                });
    

    for update a data .
    first create a button on place the method in it.

         updateData() {
              FirebaseFirestore.instance
                  .collection('your collection named')
                  .doc("your document id or blank it")
                  .update({
                "name": "Peterson",
                "age": 20,
              });
            }
    

    so you have to use TextField , TextEditingController, to customise your ui perfectly.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search