简体   繁体   中英

How do I create Flutter stream builder?

I have an "updateDatabase" function that retrieves the data from the Firebase Real-Time Database and saves it in the shared preferences... I put this function in the Init State of my home page but I would like this to become a stream builder so as not to having to reopen the page each time to get updated data but have them immediately.

This is part of my updateDatabase function:

updateDatabase() async {
  final prefs = await SharedPreferences.getInstance();

  DatabaseReference starCountRef =
      FirebaseDatabase.instance.ref('/utenti/$uid/');
  starCountRef.onValue.listen((DatabaseEvent event) {
    final data = event.snapshot.value as Map;
    prefs.setInt("videoAgosto2022", data["videoAgosto2022"]);
ect...
}

This is part of my home page...

Widget build(BuildContext context) { double altezza = MediaQuery.of(context).size.height;

  return StreamBuilder(
        stream: What should I put here?,
        builder: (context, snapshot) => SingleChildScrollView(
              child: Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Column(

What should I put in the "dada" field?

A stream function must be marked as async*

Then, you just have to put your function in the stream attribute of the StreamBuilder widget

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM