0

I have some data in the form of objects on my firebase rtdb, I just want to retrieve the data on my device and save it using the persistence method. I've been on this for a month now and looked out for a lot of possible solutions but that doesn't seem to help. Here is the structure of my data:

(Let me know if you require any missing piece of information) enter image description here

Thanks in advance!

Edit #1

This is what I tried

class contact{
 final String name,desig,category,ip_direct,ip_office,ip_res,ll_off,ll_res,mob;

 contact({this.name,this.desig,this.category,
   this.ip_direct,this.ip_office,this.ip_res,this.ll_off,this.ll_res,this.mob});

 factory contact.fromJson(Map<dynamic, dynamic> json){
  return contact(
    name: json["Name"],
    desig: json["Design"],
    category: json["Category"],
    ip_direct: json["IP Direct"].toString(),
    ip_office: json["IP Office"].toString(),
    ip_res: json["IP Home"].toString(),
    ll_off: json["LL Office"].toString(),
    ll_res: json["LL Home"].toString(),
    mob: json["Mobile"].toString(),
  );
 }
}

Future<List<contact>> getList()async{
    final dbRef = FirebaseDatabase.instance.reference().child("Deans");
    List<contact> list=new List();
    DataSnapshot snap=await dbRef.once();
    List<dynamic> obj=snap.value;

    for(int i=0; i<obj.length; i++){
      list.add(contact.fromJson(obj[i]));
    }
    return list;
  }

I cannot persist data here

4
  • Add your efforts, please show your code. Commented Jul 1, 2021 at 6:06
  • Go to here firebase.flutter.dev/docs/firestore/usage Commented Jul 1, 2021 at 6:07
  • @Vinit_Saini you've shared the link for Firestore, which is diff from Realtime DB Commented Jul 1, 2021 at 6:57
  • @Vinit_Saini I've added some code segments Commented Jul 1, 2021 at 7:05

2 Answers 2

2

I had the same problem and could not find the solution, until I looked inside the library file and the solution was simpler than I thought.

....
final Future<bool> db = FirebaseDatabase().setPersistenceEnabled(true);

final DatabaseReference _fechasRef =
  FirebaseDatabase.instance.reference().child('...');
....

The first line communicates that the data must be stored, because in firebase realtime database data persistence is not enabled by default.

Sign up to request clarification or add additional context in comments.

Comments

0

Firebase realtime database is persistent by default and your code will work even off line once the data has been loaded at least once.

1 Comment

I believe what you're talking about is firestore, because i didn't find any such thing in realtime database. There is a feature for offline persistence in realtime DB, but by default it's turned off

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.