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

I have to refresh page for getting data in flutter listview.builder

I am fetching records from firebase collection with a UDF and calling this UDF inside Widget build,

it has to refresh to show data,

looks like. widget build before getting data….how to prevent loading widget build until data is not fetched …

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


class _DemoPage2State extends State<DemoPage2> {

  List<String> list = [];

  void getusers() async
  {
    final snapshot = await FirebaseFirestore.instance
        .collection("users").get();

    list.clear();
    for (int x = 0; x < snapshot.docs.length; x++) {
      list.add(snapshot.docs[x].id);
    }
  }


  @override
  Widget build(BuildContext context) {
    getusers();
    return Scaffold(
      body: ListView.builder(
          itemCount: list.length,
          itemBuilder: (context, index) {
            return Container(
                padding: EdgeInsets.all(20),
                child: Text(list[index].toString()));
          }),

    );
  }
}

>Solution :

Try using StreamBuilder

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: StreamBuilder(
      stream: myStream,
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return ListView.builder(itemBuilder: itemBuilder);
        }
        return CircularProgressIndicator();
      },
    ));
  }
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