Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to have entry variable in a stateful widget?

I’m just beginning to use firestore, and in the same time learning to use flutter and dart.
I have two widgets : a stateless one and a stateful one, both returning the age of a user in a collection ‘users’. The stateless one has the user id as argument, but cannot handle real time changes. I don’t know how to modify the stateful one, which does handle real time changes, so that it takes the user id as argument as well.
The stateless one :

class GetAge extends StatelessWidget {
  final String documentId;

  const GetAge(this.documentId);

  @override
  Widget build(BuildContext context) {
    CollectionReference users =
        FirebaseFirestore.instance.collection('Users');

    return FutureBuilder<DocumentSnapshot>(
      future: users.doc(documentId).get(),
      builder:
          (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
        if (snapshot.hasError) {
          return const Text("Something went wrong");
        }

        if (snapshot.hasData && !snapshot.data!.exists) {
          return const Text("Document does not exist");
        }

        if (snapshot.connectionState == ConnectionState.done) {
          Map<String, dynamic> data =
              snapshot.data!.data() as Map<String, dynamic>;
          return Text('Age : ${data['age']} €');
        }

        return const Text("loading");
      },
    );
  }
}

The statefull one :

class getAgeRT extends StatefulWidget {
  const getAgeRT({super.key});

  @override
  State<getAgeRT> createState() => _getAgeRTState();
}

class _getAgeRTState extends State<getAgeRT> {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder<DocumentSnapshot<Map<String, dynamic>>>(
        stream: FirebaseFirestore.instance
            .collection('Users')
            .doc('User1')
            .snapshots(),
        builder: (BuildContext context,
            AsyncSnapshot<DocumentSnapshot<Map<String, dynamic>>> snapshot) {
          if (snapshot.hasError) {
            return const Text('Something went wrong');
          }

          if (snapshot.connectionState == ConnectionState.waiting) {
            return const Text("Loading");
          }

          Map<String, dynamic> data =
              snapshot.data!.data() as Map<String, dynamic>;
          return Text(" age :  ${data['age']} ");
        });
  }
}

I tried to copy the stateless one’s model to the stateful one but with no success… I don’t think it’s related to firestore, just a lack of practice from me ! thanks in advance !

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The StatefulWidget can receive args too.

class getAgeRT extends StatefulWidget {
  const getAgeRT({required this.documentId, super.key});

  final String documentId;

  @override
  State<getAgeRT> createState() => _getAgeRTState();
}

class _getAgeRTState extends State<getAgeRT> {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder<DocumentSnapshot<Map<String, dynamic>>>(
        stream: FirebaseFirestore.instance
            .collection('Users')
            .doc(widget.documentId)
            .snapshots(),
            ...
}

You can access the args with widget. before

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading