【问题标题】:How to retrieve multiple fields in an index of an array in firestore using Flutter如何使用 Flutter 在 Firestore 中检索数组索引中的多个字段
【发布时间】:2020-04-02 02:34:59
【问题描述】:

在下面的链接中,我在 Firestore 中有一些数据。

我的 Firestore 结构:

我在约会提醒时间字段中有一个数组。这些索引中的每一个都有 MedicationName、notificationType 和 time。我有一个名为“查看提醒”的功能,它向我的用户显示他们之前设置的所有提醒。我希望将 MedicationName、notificationType 和时间作为字符串传递,并且用户能够看到他们的提醒。

这是我写的一些代码sn-p

查看AppointmentReminders.dart

            ListView.builder(
              physics: const NeverScrollableScrollPhysics(),
              shrinkWrap: true,
              itemCount: user.appointmentName.length,
              itemBuilder: (BuildContext context, int index) {
                return appointmentDataitems(user.appointmentName[index],context);
              },
            ),


//List tile for appointment name and then appointment value
Container appointmentDataitems(String appointmentName, BuildContext context) {
  return Container(
    height: 40.0,
    child: ListTile(
      leading: Text(
        "Appointment name:",
      ),
      trailing: Text(
        appointmentName,
      ),
    ),
  );
}

user.dart

class User 
{
  final List<dynamic> appointmentName;

  User(
      {
      this.appointmentName,
      });
}

总之,我想使用 Flutter 动态显示上面的数据,以便用户查看他们的预约/用药提醒

提前致谢!

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    您的用户预约模式如下:

    class User{
      List<Appointment> appointments;
      User({this.appointments});
    
      Map<String, dynamic> toJson() => {
        "appointmentReminderTime": this.appointments
      };
    
    
      User.fromJson(Map jsonMap)
          : appointments =   (jsonMap['appointmentReminderTime'] as List)
          ?.map((i) => Appointment.fromJson(i))
          ?.toList();
    
    }
    
    class Appointment {
      final String medicationName;
      final String notificationType;
      final Timestamp time;
    
      const Appointment(this.medicationName, this.notificationType,this.time);
    
      Map<String, dynamic> toJson() => {
        "MedicationName": this.medicationName,
        "notificationtype": this.notificationType,
        "time": this.time
      };
    
      Appointment.fromJson(Map jsonMap)
          : medicationName = jsonMap['MedicationName'],
            notificationType = jsonMap['notificationtype'],
            time = jsonMap['time'];
    }
    

    您将使用 User 类型从 firestore 获取数据,然后您将在 UI 中显示这些数据。

    ListView.builder(
              physics: const NeverScrollableScrollPhysics(),
              shrinkWrap: true,
               itemCount: user.appointments.length,
                   itemBuilder: (BuildContext context, int index) {
                       return appointmentDataitems(user.appointments[index],context);
                        },
          ),
    
    
    //List tile for appointment name and then appointment value
    Container appointmentDataitems(Appointment appointment, BuildContext context) {
      return Container(
        height: 40.0,
        child: ListTile(
          leading: Text(
            appointment.medicationName,
          ),
          trailing: Text(
            appointment.notificationType,
          ),
        ),
      );
    }
    

    【讨论】:

    • 好的,谢谢,如果我的用户类中没有其他内容,这将起作用。我在用户类中定义了其他对象,并且约会是唯一具有 jsonMap 的对象,这给了我一个错误,告诉我其他对象未由该 User.fromJson 构造函数初始化。我该如何解决这个问题?
    • 将此用户类重命名为 UserData,然后创建另一个名为 User 的类并在其中声明最终用户用户;这样,您也可以在 User 类中拥有其他对象。
    猜你喜欢
    • 2021-06-13
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    • 2021-04-17
    • 2022-01-18
    • 2020-07-12
    • 1970-01-01
    相关资源
    最近更新 更多