siva kumar
siva kumar

Reputation: 2925

store data to models from firebase in flutter

I am trying to store data in array of model from firebasedatabase

my firebasedatabase response look like this:

 {-Ku3EYNaR_E5XVhAij2w: {customerAddress: Ram, customerMobileNumber: 921212121211, CustomerName: Ram, customerImageURL: }, -Ku3F6_0jro_5CI7FDsY: {customerAddress: Qweqweqwe, customerMobileNumber: 888888, CustomerName: Siva, customerImageURL: }}

and my model look like this

   class listOfUsers {
   String name = '';
    String phoneNumber = '';
    String address = '';
   listOfUsers(this.name,this.phoneNumber,this.address);
   }


 List<listOfUsers>userInformation_ = <listOfUsers>[];
 getusersinfo() async{
 FirebaseUser user = await FirebaseAuth.instance.currentUser();

 if (user != null) {
  _messagesRef
    .child(user.uid)
    .child('x')
    .onValue
    .listen((Event event) {
  print('value: ${event.snapshot.value}');
  var a = event.snapshot.value;

  for(var child in event.snapshot.value){
    print(child);
    }



   });
   }
   }

   getting error as 
   [VERBOSE-2:dart_error.cc(16)] Unhandled exception:
    NoSuchMethodError: Class '_InternalLinkedHashMap' has no instance      getter 'iterator'.
   Receiver: _LinkedHashMap len:2

can any one help me to store data from firebase database to models in flutter

Upvotes: 0

Views: 3684

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657238

You can either write a serialization method and deserialization constructor manually that creates JSON from the class instance / takes the JSON and assigns the values to the instance fields

or you can use one of the code generation solutions to get that code generated automatically

Upvotes: 2

Related Questions