方法' fromsnapshot'为类型定义的IS不定义了病人'飞镖扑来

发布于 2025-02-06 06:13:50 字数 1820 浏览 1 评论 0原文

试图将数据从Firebase加载到数据表 我有一个错误:

未针对“患者”类型定义的方法“ 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,
  };}}

trying to load data from firebase to a data table
i got this error :

The method 'fromSnapshot' isn't defined for the type 'Patient'.

This is the function where i have this problem , it show on '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();
    });
  }

and this is the class Patient that I call

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,
  };}}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故笙诉离歌 2025-02-13 06:13:50

尝试添加一种方法:

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

Try adding a method instead:

static Patient fromSnapshot(DocumentSnapshot snapshot) {
    Patient _newPatient = Patient.fromJson(snapshot.data()! as Map<String, dynamic>);
    _newPatient.reference = snapshot.reference;
    return _newPatient;
}
无力看清 2025-02-13 06:13:50

确保您的班级看起来像这样:

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;
  }

}

Make sure your class looks something like this:

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;
  }

}

Or this:

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;
  }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文