Home > Mobile >  retreaved null from realtime database firebase (flutter)
retreaved null from realtime database firebase (flutter)

Time:02-03

I want to get user data from real-time database and retreaved null inside my app, I make an app and i want to put userdata inside his account but i get null

my data model

class AdminData{
  String? id;
  String? email;
  String? name;
  String? phone;

  AdminData({this.email, this.name, this.phone});

  AdminData.fromSnapshot(DataSnapshot dataSnapshot)
  {
    id = dataSnapshot.key;
    email = dataSnapshot.value("email");
    name = dataSnapshot.value("name");
    phone = dataSnapshot.value("phone");
  }
}

at the main

DatabaseReference adminRef = FirebaseDatabase.instance.reference().child("admin");

here my global var

String mapKey = "AIzaSyBCbiDMiUQ3VHdH8RFnCdDZC_tN0ITScIo";
User? currentfirebaseUser;
AdminData adminCurrentInfo = AdminData();

int driverRequestTimeOut = 40;
String statusRide = "";
String carDetail = "";
String driverName = "";
String driverPhone = "";
String driverKey = "";
String serverToken = "key=AAAAi0I5-DE:APA91bEPa3vsYU2YU1WUzXHEWL_djQgbP0-ps59l-Mght-UK0l7f-wQKjDXULjWAb-G789gbZEVlM6i88whUN6qK4hZk8yG_9Mgg4aLcuWt4bZF1O5aAsdgJRJX1GjLfeVQuhMKU1jYG";

my function where i get admin infromation firebase database

  static void getCurrentOnlineAdminInfo() async
  {
    currentfirebaseUser = await FirebaseAuth.instance.currentUser;
    String userId = currentfirebaseUser!.uid;
    print("current user name ======== ${userId}");

    //DatabaseReference reference = FirebaseDatabase.instance.reference().child("admin").child(userId);

    adminRef.child(userId).once().then((DataSnapshot dataSnapshot)
    {
      if(dataSnapshot.value != null){
        adminCurrentInfo = AdminData.fromSnapshot(dataSnapshot);
        print("current user name ======== ${adminCurrentInfo.name}");
      }
    });
  }

and i call the function inside initstate

  @override
  void initState(){
    // TODO: implement initState
    super.initState();
    polylinePoints = PolylinePoints();
    AssistantMethods.getCurrentOnlineAdminInfo();
    print(adminCurrentInfo.name);
  }

my database looks

admin
5qH6CxYdu0WYrevcDDY.......
   email: "bish@.."
   name: "bishoy"
   password: "....."
   phone: "....."

then i received this

E/flutter (12780): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: NoSuchMethodError: Class '_InternalLinkedHashMap<Object?, Object?>' has no instance method 'call'.
E/flutter (12780): Receiver: _LinkedHashMap len:4
E/flutter (12780): Tried calling: call("email")
E/flutter (12780): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:68:5)
E/flutter (12780): #1      new AdminData.fromSnapshot (package:free_now_taxi/Admin_app/Models/admin_request_firebase.dart:15:31)
E/flutter (12780): #2      AssistantMethods.getCurrentOnlineAdminInfo.<anonymous closure> (package:free_now_taxi/Admin_app/Assistants/assistantMethods.dart:53:38)
E/flutter (12780): #3      _rootRunUnary (dart:async/zone.dart:1436:47)
E/flutter (12780): #4      _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter (12780): <asynchronous suspension>
E/flutter (12780): 

CodePudding user response:

You used curved brackets () in your model instead of []

Try this:

class AdminData{
  String? id;
  String? email;
  String? name;
  String? phone;

  AdminData({this.email, this.name, this.phone});

  AdminData.fromSnapshot(DataSnapshot dataSnapshot)
  {
    id = dataSnapshot.key;
    email = dataSnapshot.value["email"];
    name = dataSnapshot.value["name"];
    phone = dataSnapshot.value["phone"];
  }
}
  •  Tags:  
  • Related