The argumnet type 'Null Funcion(DataSnapshot)' cnt be assigned to the parameter type 'Future Or <dynamic> Function(DataBaseEvent)'

1,267

The once method returns a DatabaseEvent, not a DataSnapshot. DatabaseEvent is a class that encapsulates a DataSnapshot AND a DatabaseEventType, to extract the snapshot, you must use DatabaseEvent.snapshot:

reference.once().then((event) {
  final dataSnapshot = event.snapshot;
  if (dataSnapShot!.value != null) {
    userCurrentInfo = Users.fromSnapshot(dataSnapshot);
  }
});

Here is another solution I think might do what you want:

// async methods should return a future
static Future<void> getCurrentOnLineUserInfo() async {
    firebaseUser = await FirebaseAuth.instance.currentUser;
    String userId = firebaseUser!.uid;
    DatabaseReference reference =
        FirebaseDatabase.instance.ref().child("user").child(userId);

    final snapshot = await reference.get(); // you should use await on async methods
    if (snapshot!.value != null) {
      userCurrentInfo = Users.fromSnapshot(snapshot);
    }
  }
}
Share:
1,267
mobdev991
Author by

mobdev991

Updated on January 02, 2023

Comments

  • mobdev991
    mobdev991 over 1 year

    I have this function that is giving me an error.

    the getCurrentOnLineUserInfo function is trying to get read data from the Firebase Database of the current user that is logged in.

    The argument type 'Null Funcion(DataSnapshot)' can't be assigned to the parameter of type 'Future Or Function(DataBaseEvent)'

    I am following a year-old tutorial, so the issue might be the code is old. I might need new syntax or something.

    static void getCurrentOnLineUserInfo() async {
        firebaseUser = await FirebaseAuth.instance.currentUser;
        String userId = firebaseUser!.uid;
        DatabaseReference reference =
            FirebaseDatabase.instance.ref().child("user").child(userId);
    
        print("getCurrentOnLineUser info executed!");
    
        print('${firebaseUser!.email}${firebaseUser!.displayName}');
        
        // errors below this
    
        reference.once().then((DataSnapshot dataSnapshot) {
          if (dataSnapShot!.value != null) {
            userCurrentInfo = Users.fromSnapshot(dataSnapshot);
          }
        });
    
      }
    }
    

    and here is my class that is assigning data. This class is giving no errors

    class Users {
      String? id;
      String? email;
      String? phone;
      String? name;
    
      Users({this.id, this.email, this.phone, this.name});
    
      Users.fromSnapshot(DataSnapshot dataSnapshot) {
        id = dataSnapshot.key!;
    
        var data = dataSnapshot.value as Map?;
    
        if (data != null) {
          email = data?["email"];
          name = data?["name"];
          phone = data?["phone"];
        }
      }
    }
    
  • mobdev991
    mobdev991 over 2 years
    Thank you for the help.. error is gone but still not reading data
  • mobdev991
    mobdev991 over 2 years
    still getting error userCurrentInfo is null...
  • h8moss
    h8moss over 2 years
    @mobdev991 I am sorry, I am not knowledgable on realtime database, thus my answer can only come from the little knowledge I have and from reading the docs, regardless, I have edited my answer to show a different possible solution, could you check if that one works?