2
Hello, I’m trying to filter orders by the current date, but when I try to use Where in the firebase’s Collection, my Listen simply stops working. Sending no new update to the list.
In a totally strange way, when I remove the Where the Listen comes back to function normally.
Has anyone been through the same problem or knows a possible solution?
Listen firebase:
Future<void> addPedidosListener() async{
var now = new DateTime.now();
var formatter = new DateFormat('dd-MM-yyyy');
String dataAtual = formatter.format(now);
print(dataAtual);
Pedido pedido;
FirebaseFirestore.instance
.collection("pedidos")
.doc("Kwdqc77luBnyPdS2AqD4")
.collection("pedidos")
.where("dataPedido", isEqualTo: dataAtual)
.orderBy("numeroPedido", descending: false)
.snapshots()
.listen((snapshot) {
snapshot.docChanges.forEach((change) {
String pedidoId = change.doc.id;
pedido = Pedido.fromDocumentSnapshot(change.doc);
switch(change.type){
case DocumentChangeType.added:
//========== quando um pedido for adicionado ==========
print('novo pedido');
//pedidosModel.addPedido(pedido);
pedidosModel.insertPedido(0, pedido);
break;
case DocumentChangeType.modified:
//========== modificado ==========
Pedido pedidoGlobal = Pedido.recuperarPedidoGlobal();
if(pedidoGlobal != null && pedido.idPedido == pedidoGlobal.idPedido){
Pedido.setarPedidoGlobal(pedido);
}
pedidosModel.listPedidos.indexWhere((element) {
if(element.idPedido == pedido.idPedido){
int index = pedidosModel.listPedidos.indexOf(element);
pedidosModel.attPedido(index, pedido);
}
return element.idPedido == pedido.idPedido;
});
break;
case DocumentChangeType.removed:
//========== removido ==========
pedidosModel.removeWhere(pedidoId);
break;
}
});
//_sort();
});
}
build:
@override
Widget build(BuildContext context) {
final _pedidosBloc = BlocProvider.of<PedidosBloc>(context);
return Observer(
builder: (_){
return ListView.builder(
//reverse: true,
itemCount: pedidosModel.listPedidos.length,
itemBuilder: (context, index){
return CardPedido(pedidosModel.listPedidos[index]);
},
);
},
);
}
mobx:
part 'PedidosModel.g.dart';
class PedidosModel = _PedidosModel with _$PedidosModel;
abstract class _PedidosModel with Store{
@observable
ObservableList<Pedido> listPedidos = ObservableList();
@action
void addPedido(Pedido value){
listPedidos.add(value);
}
@action
void insertPedido(int index, Pedido value){
//listPedidos.removeWhere((pedido) => pedido.idPedido == value.idPedido);
listPedidos.insert(index, value);
}
@action
void attPedido(int index, Pedido value){
listPedidos.removeWhere((pedido) => pedido.idPedido == value.idPedido);
listPedidos.insert(index, value);
}
@action
void removeWhere(pedidoId){
listPedidos.removeWhere((pedido) => pedido.idPedido == pedidoId);
}
}
A few points for you to review... Ta weird the way you made your Isten, would it not be better to just do
[...].collection("pedidos").where("dataPedido", isEqualTo: dataAtual)[...]
? And also see in your bank how the date is being saved, if I’m not mistaken Firebase uses Timestamp, then you would need to take this into consideration when filtering the date to return correctly.– Matheus Ribeiro
The Isten is correct, because I need the company ID first. What about the date, when filter for the first time it actually brings only the items of the current date, however, when updating some data Listen is not notified automatically, what should happen.
– Alex Anderson
I have been researching, so I saw it is only possible to use the Where condition together with the orderby if they point to the same field. The challenge now is to figure out how to make these filters separately. source: https://stackoverflow.com/questions/58419902/where-and-orderby-filter-not-working-together-when-filtering-firebase-data
– Alex Anderson
My friend, aren’t we missing the creation of an index there in the Firestore? Come on: when you use "ordering" with Where, in some cases you need indexes (as mentioned in the Firebase doc).
– Mateus