【问题标题】:Flutter Firebase merge two collections data in one single array and display data in widgetFlutter Firebase 将两个集合数据合并到一个数组中并在小部件中显示数据
【发布时间】:2021-07-10 17:17:29
【问题描述】:

我想将两个集合数据合并到一个数组中并在 listView.builder 中显示结果

用户(UID、姓名、电子邮件、电话、经度、纬度),

and trips(userId, date, time, price, from, to)。

PS:这两个集合之间的关系是用户ID,

所以我想为每个元素显示一张卡片,其中包含以下详细信息:

姓名、电子邮件、电话、日期、时间、从、到。

如何实现?

    import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';

class VoyagerModel {
  var firestore = FirebaseFirestore.instance;
  String de, vers, adress;
  DateTime dateTime;
  double price;
  User user;
  String time;
  createVoyage() async {
    await firestore.collection('voyages').add({
      'userId': this.user.uid,
      'de': this.de,
      'vers': this.vers,
      'date': this.dateTime,
      'time': this.time,
      'adress': this.adress,
      'prix': this.price
    });
  }
}

和用户模型

import 'package:cloud_firestore/cloud_firestore.dart';

class ClientModel {
  String ref = 'clients';

  createUser(
      {id,
      int number,
      String name,
      String email,
      double long,
      double lat}) async {
    await FirebaseFirestore.instance.collection('users').doc(id).set({
      'userId': id,
      'name': name,
      'email': email,
      'phone': number,
      'longitude': long,
      'latitude': lat
    }).catchError((err) => {print(err.toString())});
  }
}

【问题讨论】:

  • VoyagerModel 中的UserClientModel 相同?
  • 是一样的,它们之间的关系是多对多的

标签: firebase flutter google-cloud-firestore


【解决方案1】:

这就是你要找的东西?

List<VoyagerModel> voyagers = [];
List<User> users = [];

@override
void initState() {
  // Fill voyagers and users
  super.initState();
}

@override
  Widget build(BuildContext context) {
    return Container(
      child: ListView.builder(
        itemCount: users.length,
        itemBuilder: (context, index) {
          final user = users[index];
          final userVoyagers =
              voyagers
               .where((voyager) => voyager.user.id == user.id)
               .toList();

          return Column(
            children: userVoyagers.map((userVoyager) {
              return Card(
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text(user.name),
                    Text(user.email),
                    Text('${user.number}'),
                    Text(userVoyager.dateTime.toString()),
                    Text(userVoyager.de),
                    Text(userVoyager.vers),
                  ],
                ),
              );
            }).toList(),
          );
        },
      ),
    );
  }
}

【讨论】:

  • 用户集合中的姓名、电子邮件、电话;航程收集日期和日期
  • VoyagerModel 有一个包含姓名、电子邮件和电话的用户。我在每次迭代中访问分配给航海者的用户
  • 不,它们是不同的集合,它们是独立的,它们之间的关系只是用户ID,它们是多对多的
  • 好的。我想你有两个不同的List。一个是List&lt;VoyagerModel,另一个是List&lt;User&gt;
猜你喜欢
  • 2021-02-12
  • 2015-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-29
  • 2017-03-31
  • 1970-01-01
相关资源
最近更新 更多