Zanis Ali
Zanis Ali

Reputation: 74

Flutter: I want to get data from firebase realtime database, everytime the application loads

I managed to get the data from the firebase real-time database, but I can't use it inside the app. Based on the data, I want to show different screens (Activities). For example: If the user paid then show the "Paid" screen, otherwise show the "Pay Now" screen. I couldn't use the variable used inside eg: paid_status , outside from the below scope. So, I declared it as a global variable in another file.

package_stream.listen((DatabaseEvent event) {
  globals.paid_status = event.snapshot.value;
});

With the above code, it works but every time, the user closes the app and login again, the variable value is changed or kept old (after the database is updated). I want to get the value from the database every time the user opens the app or logout and login again.

I used below code to get the data:

setState(() {
 
  DatabaseReference paid_stat = db_reference.child('Records/${uid}/Paid');
  // Get the Stream
  Stream<DatabaseEvent> package_stream = paid_stat.onValue;
  // Subscribe to the stream!
  package_stream.listen((DatabaseEvent event) {
    globals.paid_status = event.snapshot.value;
  });
});

Upvotes: 0

Views: 851

Answers (1)

Zanis Ali
Zanis Ali

Reputation: 74

I solved it myself :

  1. I created an async Future function using get() to return the snapshot.
  2. Using the snapshot.value, I showed different screens/activities.

Upvotes: 1

Related Questions