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

Ayush Nanglia
  • 89
  • 1
  • 3
  • 10

2 Answers2

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.

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.

gbaccetta
  • 4,449
  • 2
  • 20
  • 30
  • 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 – Ayush Nanglia Jul 01 '21 at 06:56