繁体   English   中英

Flutter Firebase 遍历匹配字段值的所有文档,并将每个文档的字段插入字符串列表

[英]Flutter Firebase Iterate through all documents that matches field value and inserting field from each one into a List of String

我试图从 firebase 中的文档中获取一个字段值到一个字符串列表中,这些文档与我自己的值中的字段值的 WHERE 相匹配。 (我想在彼此之上显示图像以根据数据创建完整图片,准确地说使用两个字段然后创建图像列表以显示为位于堆栈内)我的代码:植入 Class:

class Implant {
  final String id;
  final String pid;
  final String aptid;
  late String type;
  late double? sizew;
  late int? sizel;
  late String? positionQ;
  late int? positionN;

  Implant(this.id, this.pid, this.aptid, this.type, this.sizew, this.sizel,
      this.positionQ, this.positionN);

  Map<String, dynamic> toJson() => {
        'ID': id,
        'PID': pid,
        'APTID': aptid,
        'TYPE': type,
        'SIZEW': sizew,
        'SIZEL': sizel,
        'POSITIONQ': positionQ,
        'POSITIONN': positionN,
      };

  factory Implant.fromJson(Map<String, dynamic> json) {
    return Implant(
      json['ID'],
      json['PID'],
      json['APTID'],
      json['TYPE'],
      json['SIZEW'],
      json['SIZEL'],
      json['POSITIONQ'],
      json['POSITIONN'],
    );
  }

  static Map<String, dynamic> toMap(Implant implant) => {
        'ID': implant.id,
        'PID': implant.pid,
        'APTID': implant.aptid,
        'TYPE': implant.type,
        'SIZEW': implant.sizew,
        'SIZEL': implant.sizel,
        'POSITIONQ': implant.positionQ,
        'POSITIONN': implant.positionN,
      };
  static String encode(List<Implant> implant) => json.encode(
        implant
            .map<Map<String, dynamic>>((implant) => Implant.toMap(implant))
            .toList(),
      );
  static List<Implant> decode(String implants) =>
      (json.decode(implants) as List<dynamic>)
          .map<Implant>((implant) => Implant.fromJson(implant))
          .toList();
}

字符串 function 列表的未来:

static Future<List<String>> getImplants(Patient patient) async {
    List<String> result = ['assets/images/teeth/empty.png'];
    var collection = FirebaseFirestore.instance.collection('implants');
    var docSnapshot = await collection.doc(patient.id).get();
    if (docSnapshot.exists) {
      Map<String, dynamic> data = docSnapshot.data()!;
      result.add(data['POSITIONQ'] + data['POSITIONN']);
    }
    return result;
  }

我如何将它们转换为 Stack 和 Positioned:

static Future<void> showPatientImplants(BuildContext context, Patient patient,
      {bool spaces = true}) async {
    List<String> myImplants;
    myImplants = await getImplants(patient);
    List<Widget> x = [Image.asset('assets/images/teeth/empty.png')];
    myImplants.forEach((element) {
      x.add(Image.asset(element));
    });

    return await showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            scrollable: true,
            content: SingleChildScrollView(
              child: GestureDetector(
                onTap: () {
                  log(myImplants.toString());
                },
                child: Stack(
                  children: x,
                ),
              ),
            ),
          );
        });
  }

我认为我面临的唯一问题是 Future<List> function 没有为我提供所需的值。

我尝试使用我在 SO 和谷歌上找到的其他功能,但没有任何效果。 我想我可以尝试使用 stream 和 Listview.builder 来获取文件名,但似乎不应该这样做。 任何帮助表示赞赏。

没关系使用以下代码解决它:

static Future<List<String>> getImplants(Patient patient) async {
    List<String> result = ['assets/images/teeth/empty.png'];
    log('ID:${patient.id}');
    var collection = FirebaseFirestore.instance
        .collection('implants')
        .where('PID', isEqualTo: patient.id);
    var docSnapshot = await collection.get();
    for (var element in docSnapshot.docs) {
      if (element.exists) {
        Map<String, dynamic> data = element.data();
        result.add(data['POSITIONQ'].toString() + data['POSITIONN'].toString());
      }
    }

    return result;
  }

但如果有人有更好的主意,我会和所有有类似问题的人一起感激。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM