Home > OS >  Instance members can't be accessed from a factory constructor. Flutter Firebase
Instance members can't be accessed from a factory constructor. Flutter Firebase

Time:06-09

I have two problems in my code , the first one is showing on _patientFromJson and second one is The argument type 'Object' can't be assigned to the parameter type 'Map<String, dynamic>' that showing on snapshot.data()!

class Patient{
  String? name ; 
  String? pass ; 
  String? image ; 
  String? genre ; 
  DateTime? birth ; 

  Patient(this.name, this.pass, this.genre,
      this.birth,
      {this.reference});



  DocumentReference? reference;
  factory Patient.fromSnapshot(DocumentSnapshot snapshot) {
    Patient newPatient = Patient.fromJson(snapshot.data()!);
    newPatient.reference = snapshot.reference;
    return newPatient;
  }
  factory Patient.fromJson(Map<String, dynamic> json) =>
      _patientFromJson(json);

  Map<String, dynamic> toJson() => _patientToJson(this);

@override
  String toString() => 'name $name';

  Patient _patientFromJson(Map<String, dynamic> data) {
  return Patient(
    data['name'],
    data['pass'],
    data['birth'],
    data['genre'],
  );
}
Map<String, dynamic> _patientToJson(Patient instance) {
  return {
    'name' : instance.name,
    'pass': instance.pass,
    'birth': instance.birth,
    'genre': instance.genre,
  };

}
}

CodePudding user response:

Convert your snapshot data to map data

Patient newPatient = Patient.fromJson(snapshot.data()! as Map<String, dynamic>');

Updated for the first one

Try removing the reference to the instance member. And you may try with

  factory Patient.fromJson(Map<String, dynamic> data) =>
      Patient(
          data['name'],
          data['pass'],
          data['birth'],
          data['genre'],
      );

For more about instance_member_access_from_factory read this documentation

  • Related