Home > OS >  Why is my Flutter App not getting the updated data in Firebase Realtime Database?
Why is my Flutter App not getting the updated data in Firebase Realtime Database?

Time:11-04

Currently, I am working on a flutter project that uses Realtime Database in Firebase. This app has an update functionality for the details of the user. I have successfully implemented the updating functionality as it is being stored in the database right after the update. However, when I go back to the View Profile Page, the details that are being displayed are not yet updated. I need to go to some other tab first and then go back to the View Profile page where the updated data will then be loaded. Why there are delays, shouldn't it be real-time, once I updated the data? Any help will be much appreciated. Thanks!

Here is my code for displaying the current user's information:

static void readCurrentOnlineUserInfo() async
  {

    final FirebaseAuth fAuth = FirebaseAuth.instance;
    User? currentFirebaseUser;

    currentFirebaseUser = fAuth.currentUser;

    DatabaseReference userRef = FirebaseDatabase.instance
        .ref()
        .child("passengers")
        .child(currentFirebaseUser!.uid);

    userRef.once().then((snap)
    {
      if(snap.snapshot.value != null)
      {
        userModelCurrentInfo = UserModel.fromSnapshot(snap.snapshot);
        print("name"   userModelCurrentInfo!.first_name.toString());
        print("username"   userModelCurrentInfo!.username.toString());
      }
    });
  }

On the other hand, this is the code that I am using for displaying the data into my app:

import 'package:firebase_database/firebase_database.dart';

class UserModel
{
  String? first_name;
  String? last_name;
  String? id;
  String? email;
  String? username;
  String? password;
  String? phoneNum;

  UserModel({this.first_name, this.last_name, this.id, this.email, this.username, this.password, this.phoneNum});

  UserModel.fromSnapshot(DataSnapshot snap)
  {
    first_name = (snap.value as dynamic)["first_name"];
    last_name = (snap.value as dynamic)["last_name"];
    id = snap.key;
    email = (snap.value as dynamic)["email"];
    username = (snap.value as dynamic)["username"];
    password = (snap.value as dynamic)["password"];
    phoneNum = (snap.value as dynamic)["phoneNum"];
  }
}

I display the above information in widgets like this:

UserAccountsDrawerHeader(
                decoration: BoxDecoration(
                  color: Color(0xFFFED90F),
                ),
                accountName: new Text(userCurrentModelInfo!.first_name,
                  style: TextStyle( color: Colors.white,
                    fontSize: 15,
                    fontFamily: "Montserrat",
                    fontWeight: FontWeight.w600,),
                ),

CodePudding user response:

Don't Use Future Builder To get Real-time updates to use stream builder to get a real-time update

that's why you get the data when you go to another screen and come back when you use stream builder it always checks there is new data in the DB and it shows the new data

CodePudding user response:

The problem is that you use once, which reads the data once (hence its name).

If you want to get updated about changes to the data, use onValue as shown in the documentation on reading data. That also means you'll have to either store the data from the database in the state of your widget, or use a StreamBuilder to display it (since onValue returns a Stream).

For an example of using a StreamBuilder to display realtime updates from Firebase's other database, have a look at the second code snippet in this documentation on listening for realtime updates. Where that uses FirebaseFirestore.instance.collection('users').snapshots();, you'll want to use your onValue.

  • Related