[英]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 只会查询一次,即在通过
GroupModel
或FriendRequestModel
传递数据时它不会重新查询? 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.