英文:
dynamial query in firebase firestore in flutter
问题
I am trying to dynamical query in firestore for <= and >= operator.
that is mean I wanna add each key and value dynamical to the query
but I getting an error that it will not work on firebase.
> All where filters with an inequality (<, <=, >, or >=) must be on the same field. But you have inequality filters on 'FieldPath([price])' and 'FieldPath([meter])'.
'package:cloud_firestore/src/query.dart':
Failed assertion: line 780 pos 13: 'hasInequality == field'
My code:
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
late Query _query;
final Map<String, dynamic> _filters = {
'price': 500,
'meter': 120,
};
@override
void initState() {
super.initState();
_query = FirebaseFirestore.instance.collection('items');
for (var key in _filters.keys) {
if (_filters[key] != null) {
_query = _query.where(key, isGreaterThanOrEqualTo: _filters[key]);
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('My App'),
),
body: StreamBuilder<QuerySnapshot>(
stream: _query.snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot has Data) {
return const CircularProgressIndicator();
}
List<DocumentSnapshot> documents = snapshot data!.docs;
return ListView.builder(
itemCount: documents.length,
itemBuilder: (BuildContext context, int index) {
DocumentSnapshot document = documents[index];
return ListTile(
title: Text('${document['price']}'),
subtitle: Text(
'${document['meter']} m² / ${document['landMeter']} m²'),
);
},
);
},
),
);
}
}
is there any way to work around this limitation?
or dynamic querying?
英文:
I am trying to dynamical query in firestore for <= and >= operator.
that is mean I wanna add each key and value dynamical to the query
but I getting an error that it will not work on firebase.
> All where filters with an inequality (<, <=, >, or >=) must be on the same field. But you have inequality filters on 'FieldPath([price])' and 'FieldPath([meter])'.
'package:cloud_firestore/src/query.dart':
Failed assertion: line 780 pos 13: 'hasInequality == field'
My code:
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
late Query _query;
final Map<String, dynamic> _filters = {
'price': 500,
'meter': 120,
};
@override
void initState() {
super.initState();
_query = FirebaseFirestore.instance.collection('items');
for (var key in _filters.keys) {
if (_filters[key] != null) {
_query = _query.where(key, isGreaterThanOrEqualTo: _filters[key]);
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('My App'),
),
body: StreamBuilder<QuerySnapshot>(
stream: _query.snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) {
return const CircularProgressIndicator();
}
List<DocumentSnapshot> documents = snapshot.data!.docs;
return ListView.builder(
itemCount: documents.length,
itemBuilder: (BuildContext context, int index) {
DocumentSnapshot document = documents[index];
return ListTile(
title: Text('${document['price']}'),
subtitle: Text(
'${document['meter']} m² / ${document['landMeter']} m²'),
);
},
);
},
),
);
}
}
is there any way to work around this limitation?
or dynamic querying?
答案1
得分: 1
在Firestore文档的查询限制部分提到:
在复合查询中,范围(<、<=、>、>=)和不等于(!=、not-in)比较必须全部过滤相同的字段。
您的代码在price
和meter
字段上都添加了isGreaterThanOrEqualTo
(>=)条件,这不符合这个限制。
问题不在于您正在动态构建查询,而是您尝试在多个字段上添加isGreaterThanOrEqualTo
条件,而Firestore不支持这样做。
一个选项是在数据库中只执行一个条件,并在应用代码中进行进一步的过滤。
另一个解决方法是(通常在处理NoSQL数据库时如此)添加附加数据以允许使用这种用例,尽管我不确定是否适用于这里。
我能想到的最接近的方法是,您可以添加一个包含这两个值的单个字段:
price_meter: "500_120"
然后,您可以对这个组合值进行notEqual
查询,但要实现双重>=
可能不容易,所以客户端过滤可能是您唯一的选择。
英文:
If you check the Firestore documentation on query limitations it says:
> In a compound query, range (<, <=, >, >=) and not equals (!=, not-in) comparisons must all filter on the same field.
Your code is adding isGreaterThanOrEqualTo
(>=
) conditions on both price
and meter
fields, which does not meet this limit.
The problem is not so much that you're building your query dynamically, but that you're trying to add isGreaterThanOrEqualTo
conditions on multiple fields, which Firestore doesn't support.
One options is to perform only one condition on the database, and do further filtering in your application code.
Another workaround is (as usual when dealing with NoSQL databases) to add additional data to allow the use-case, although I'm not sure if it applies here.
The closest I can think of, you could add a single field with both of the values:
price_meter: "500_120"
You could then do a notEqual
query on this composite value, but it won't be easy to implement a double >=
this way, so client-side filtering might be your only option for that.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论