0

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?

1 Answer 1

0

A stream function must be marked as async*

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

3
  • thank you very much for the advice and actually it does not give me an error ... but the data is still updated only after reopening the page, not immediately
    – Jonathan
    Commented Aug 30, 2022 at 19:38
  • Ok I understand. Could you post a more complete sample ? It's actually hard to help you with the sample you posted
    – F Perroch
    Commented Aug 30, 2022 at 19:41
  • 1
    modifying the question and adding lines of code I understood by myself where the problem was ... I updated the shared preferences but did not update the variables connected to them. Thanks anyway you helped me, mark as solved.
    – Jonathan
    Commented Aug 30, 2022 at 20:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.