简体   繁体   English

Firebase:如果我在 Firestore 集合中查询一条记录并将一列数据传递到 model,我的应用程序是否会对下一个 model 进行第二次查询?

[英]Firebase: If I query a Firestore collection for a record and pass one column of data into a model, would my app do a second query for the next model?

I have a function called getNotifications that queries a collection in Firestore.我有一个名为getNotifications的 function,它查询 Firestore 中的一个集合。 I am running it on my Notifications screen.我在我的Notifications屏幕上运行它。

On this screen, I want to optimize the number of Firestore querying to only query once.在这个屏幕上,我想优化 Firestore 查询的次数,使其只查询一次。 When the user gets to this screen, the app should query the data once, determine the notifID for the current index, then pass the initial data into the appropriate model. If the notifID == '1' , then the initial data should be transformed via the GroupModel .当用户到达此屏幕时,应用程序应查询一次数据,确定当前索引的notifID ,然后将初始数据传递到相应的 model。如果notifID == '1' ,则应转换初始数据通过GroupModel If the notifID == '2' , then transform via the FriendRequestModel .如果notifID == '2' ,则通过FriendRequestModel进行转换。 In doing all this, am I correct in assuming that Firestore will only query once, ie it will not re-query when passing the data through either the GroupModel or the FriendRequestModel ?在执行所有这些操作时,我是否正确地假设 Firestore 只会查询一次,即在通过GroupModelFriendRequestModel传递数据时它不会重新查询? I'm worried because CommonModel only needs to read the notifID .我很担心,因为CommonModel只需要读取notifID I'm not even defining any other data fields in it, so I worry that this might signal to the Flutter framework that it needs to re-query.我什至没有在其中定义任何其他数据字段,所以我担心这可能会向 Flutter 框架发出需要重新查询的信号。

notifications.dart通知.dart

class ScreenNotifications extends StatefulWidget {
  const ScreenNotifications({Key? key}) : super(key: key);

  @override
  State<ScreenNotifications> createState() => _ScreenNotificationsState();
}

class _ScreenNotificationsState extends State<ScreenNotifications> {

void initialize() async {
  tempNotifsList = await database.getNotifications();
  setState(() {
    notifsList = tempNotifsList;
  });
}

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Notifications'),
      ),
      body: Column(
        children: [
          Expanded(
            child: ListView.builder(
              itemCount: notifsList?.length ?? 0,
              itemBuilder: (context, index) {

                final notif = CommonModel.fromJson(data);
                final notifID = notif.notifID;

                if (notifID == '1') {
                  final group = GroupModel.fromJson(data);
                }

                if (notifID == '2') {
                  final friendRequest = FriendRequestModel.fromJson(data);
                }

              }
              ...//rest of code//

database.dart数据库.dart

Future<List> getNotifications() async {
    final uid = getUID();
    List notifsList = [];
    FirebaseFirestore firestore = FirebaseFirestore.instance;
    CollectionReference notifCollection = firestore.collection('notifications_' + uid);

    final docsRef = await notifCollection.get();
    docsRef.docs.forEach((element) {
      Map<dynamic, dynamic> docMap = {'docID': element.id, 'data': element.data()};
      notifsList.add(docMap);
    });

    return notifsList;
}

the best way to go about this is to the defined a notification type as part of fields while storing your notification, go 关于此的最佳方法是在存储通知时将通知类型定义为字段的一部分,

"nofiType":....//here will be group of friends

so in your ListView.builder then you check if the notif.notiType is equl to the value show the widget所以在你的ListView.builder然后你检查 notif.notiType 是否等于显示小部件的值

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

相关问题 firebase firestore 为集合查询返回错误数据 - firebase firestore return wrong data for collection query 如何使用适用于任何 model 的方法查询 Firebase Firestore - How to query Firebase Firestore with a method that works with any model 更改现有云 Firestore 集合中的数据 model? - Changing data model in existing cloud firestore collection? 如何通过属性值查询 firebase 个文档的集合? - How do I query a collection of firebase documents by a properties value? 如何在 firebase firestore 中使用 isEqualTo 和 isNotEqualTo 查询数据 - How to query data with isEqualTo and isNotEqualTo in firebase firestore 用于活动策划应用程序的 Firestore 数据 model - Firestore data model for events planning app Cloud Firestore Firebase Collection ref 上的 IN 查询是否有 Flutter/Dart 等价物? - Is there a Flutter/Dart equivalent for the IN query on a Cloud Firestore Firebase Collection ref? Firestore 数据到 Model class - Firestore data into a Model class 如果我使用 Firebase/Firestore 查询返回值,如何返回 TableView 数据源值? - How do I return TableView datasource values if I'm using Firebase/Firestore to query the returning value? 你如何将model的用户和Firebase中的朋友收藏起来? - How would you model a collection of users and friends in Firebase?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM