Home > Back-end >  How do I create Flutter stream builder?
How do I create Flutter stream builder?

Time:08-31

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?

CodePudding user response:

A stream function must be marked as async*

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

  • Related