簡體   English   中英

沒有為“患者”飛鏢顫振類型定義“fromSnapshot”方法

[英]The method 'fromSnapshot' isn't defined for the type 'Patient' dart flutter

嘗試將數據從 firebase 加載到數據表中,我收到此錯誤:

沒有為“Patient”類型定義“fromSnapshot”方法。

這是我遇到此問題的功能,它顯示在“fromSnapshot”上

Future loadMoreDataFromStream() async {
    final stream = collection.snapshots();

    stream.listen((snapShot) async {
      await Future.forEach(snapShot.docs, (DocumentSnapshot element) {
        final Patient data = Patient.fromSnapshot(element);
        if (!patients
            .any((element) => element.name == data.name)) {
          patients.add(data);
          addDataGridRow(data);
        }
      });

      updateDataGridDataSource();
    });
  }

這就是我所說的病人類

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;
static fromSnapshot(DocumentSnapshot snapshot) {
    Patient _newPatient = Patient.fromJson(snapshot.data()! as Map<String, dynamic>);
    _newPatient.reference = snapshot.reference;
    return _newPatient;}
factory Patient.fromJson(Map<String, dynamic> data) =>
      Patient(
          data['name'],
          data['pass'],
          data['birth'],
          data['genre'],);
Map<String, dynamic> toJson() => _patientToJson(this);
@override
String toString() => 'name $name';
Patient _patientFromJson(Map<String, dynamic> json) {
  return Patient(
    json['name'],
    json['pass'],
    json['birth'],
    json['genre'],
  );}
Map<String, dynamic> _patientToJson(Patient instance) {
  return {
    'name' : instance.name,
    'pass': instance.pass,
    'birth': instance.birth,
    'genre': instance.genre,
  };}}

嘗試添加一個方法:

static Patient fromSnapshot(DocumentSnapshot snapshot) {
    Patient _newPatient = Patient.fromJson(snapshot.data()! as Map<String, dynamic>);
    _newPatient.reference = snapshot.reference;
    return _newPatient;
}

確保你的類看起來像這樣:

class Patient {
  String id = 'id'; //Add any other variable you want.

  Patient();

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

}

或這個:

class Patient {
  String id; //Add any other variable you want.

  Patient({required this.id});

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM