简体   繁体   English

Firestore 数据到 Model class

[英]Firestore data into a Model class

I have the following Firestore Database;我有以下 Firestore 数据库;

在此处输入图像描述

In my first page I query all the ' Other ' collection documents and list the fields ' name ' & ' surname ' of each document with a button that will redirect me to that person's page.在我的第一页中,我查询所有“其他”集合文档并列出每个文档的“名称”和“姓氏”字段,并使用一个按钮将我重定向到该人的页面。

class FirstPage extends StatelessWidget {
  const FirstPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<QuerySnapshot>(
      future: FirebaseFirestore.instance
          .collection('Other')
          .get(),
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.done &&
            snapshot.hasData) {
          var documents = snapshot.data!.docs;

          ListView.builder(
            itemCount: documents.length,
            itemBuilder: (context, index) {
              return TextButton(
                child: Text(documents[index]['name']),
                onPressed: () => Get.to(() => const Person()),
              );
            },
          );
        } 
      },
    );
  }
}

I know that I could simply add final parameters of ' name ' and ' surname ' into Person class like this;我知道我可以像这样简单地将“姓名”和“姓氏”的最终参数添加到Person class 中;

class Person extends StatelessWidget {
  final String name;
  final String surname;

  const Person({Key? key, required this.name, required this.surname}) : super(key: key);

But in the Person class I will have to pass that information to following pages and passing parameters through constructors back to back doesn't seem like a good practice.但是在Person class 中,我必须将该信息传递给后续页面,并且通过构造函数背靠背传递参数似乎不是一个好习惯。

From my knowledge with other programming languages I could simply generate a Model class with Setters and Getters and once a button of a person is clicked I'd set all of his information in this model class and get them in the following pages and so on. From my knowledge with other programming languages I could simply generate a Model class with Setters and Getters and once a button of a person is clicked I'd set all of his information in this model class and get them in the following pages and so on.

Would be a good practice generating a model class for this purpose in Flutter/Dart?在 Flutter/Dart 中为此目的生成 model class 是一个好习惯吗? If so, how can I generate this model class?如果是这样,我该如何生成这个 model class?

Had to use this temporary solution due lack of answers and pass this model class to every widget where the information is required.由于缺乏答案,不得不使用此临时解决方案并将此 model class 传递给需要信息的每个小部件。 My search for the best-practice on this is still going on but this also does the work.我仍在寻找这方面的最佳实践,但这也能奏效。

import 'package:cloud_firestore/cloud_firestore.dart';

class PersonModel {
  String name, surname;

  PersonModel({
    required this.name,
    required this.surname,
  });

  factory PersonModel.fromMap(Map<dynamic, dynamic> map) {
    return PersonModel(
      name: map['name'],
      surname: map['surname'],
    );
  }

  Map<dynamic, dynamic> toMap() {
    return {
      'name': name,
      'surname': surname,
    };
  }
}

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

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