'How to send the fetched data everytime to some other widget in Flutter

Wanted to pass the updated values of fetchedEntriesInApp to PasswdList widget everytime it loads.

Below is my code.

main.dart

Future fetchEntries() async {
  var fetchedEntries = [];
  var db = FirebaseFirestore.instance;
  final res = await db.collection("password_entries").get().then((event) {
    for (var doc in event.docs) {
      var resDic = {
        "entry_id": doc.id,
        "data": doc.data(),
      };
      fetchedEntries.add(resDic);
    }
  });

  return fetchedEntries;
}


class Body extends StatefulWidget {
  @override
  State<Body> createState() => _BodyState();
}

class _BodyState extends State<Body> {
  late Future fetchedEntriesInApp;

  @override
  void initState() {
    super.initState();
    fetchedEntriesInApp = fetchEntries();
  }

  void refreshEntries() {
    setState(() {
      fetchedEntriesInApp = fetchEntries();
    });
  }

  @override
  Widget build(BuildContext context) {
    setState(() {});
    return FutureBuilder(
        future: fetchedEntriesInApp!,
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return Text('Loading');
          }

          return Column(children: [
            PasswdList(fetchedEntriesInApp),
            RaisedButton(
              onPressed: () {
                Navigator.pushNamed(
                  context,
                  '/addPasswd',
                  arguments: AddPasswd(fetchEntries),
                );
              },
              child: Text('Add Psswd'),
            ),
          ]);
        });
  }
}

PasswdList Widget

class PasswdList extends StatefulWidget {
  var abc;
  PasswdList(this.abc);
  @override
  State<PasswdList> createState() => _PasswdListState();
}

class _PasswdListState extends State<PasswdList> {
  var fetchedEntriesInApp;

  @override
  Widget build(BuildContext context) {
    var entries;
    setState(() {
      entries = widget.abc;
    });
    print(entries);
    return Container(
      height: 500,
      child: ListView(
        children: [
          PasswdCard(),
        ],
      ),
    );
  }
}


Solution 1:[1]

You can add one variable for password list in your password list widget like,

class PasswdList extends StatefulWidget {
   
   var passwordlist;
  PasswdList(this.passwordlist);
  @override
  State<PasswdList> createState() => _PasswdListState();
}




class _PasswdListState extends State<PasswdList> {
  var fetchedEntriesInApp;

  @override
  Widget build(BuildContext context) {
    var entries;
    setState(() {
      entries = widget.passwordlist;
    });
    print(entries);
    return Container(
      height: 500,
      child: ListView(
        children: [
          PasswdCard(),
        ],
      ),
    );
  }
}

And you can pass it to the navigator like,

RaisedButton(
              onPressed: () {
                Navigator.push(
  context,
  MaterialPageRoute(
    builder: (context) =>PasswdList (fetchedEntriesInApp.values,
    ),
 );
              },

Solution 2:[2]

Since your PasswdList is a Stateful widget and it is embedded in your view, you can use the callback

@override
void didUpdateWidget(covariant PasswdList oldWidget) {
    super.didUpdateWidget(oldWidget);

    if (widget.abc != oldWidget.abc)
        setState(() {
            //You can have a var in your state class and re-assign it to the new value
        });
}

Note: in order for this to work, you need to re-initialize the abc list and pass it to your widget, otherwise you might need to change the if statement condition

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ABV
Solution 2 Abdallah A. Odeh