Reputation: 23
I'm using flutter and Google firestore and I need to update a collection (add a new field), when offline, I'm testing using airplane mode. When I click in the button the app crashes and the data is not sent to the database. It works fine online.
I think this happen because the async/await, however if I don't use them, the app doesn't even save when online.
Widget where the is the function that calls the update function (I'm using FutureBuilder):
var formataData = new DateFormat('y/MM/dd');
var data = formataData.format(new DateTime.now());
final _funcao = funcoesBanco();
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25)),
child: Text(
"Registrar", style: TextStyle(fontSize: 17),),
onPressed: () {
_funcao.atualizaCampos(data, 'saida1', snapshot.data[index]);
},
),
The update function:
atualizaCampos(String dataHoraAtual, String campo, DocumentSnapshot document){
Firestore.instance.runTransaction((transaction) async{
DocumentSnapshot freshSnap = await transaction.get(document.reference);
await transaction.update(freshSnap.reference , {
campo: dataHoraAtual,
});
});
}
The expected result would be the data added to the firestore, even when offline.
The errors I get in the console:
V/NativeCrypto(19561): Read error: ssl=0x740b0778c8: I/O error during system call, Software caused connection abort
V/NativeCrypto(19561): Write error: ssl=0x740b0778c8: I/O error during system call, Broken pipe
V/NativeCrypto(19561): Write error: ssl=0x740b0778c8: I/O error during system call, Broken pipe
I/OkHttpClientTransport(19561): Failed closing connection
E/flutter (19561): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(Error performing transaction, java.lang.Exception: Do transaction failed., null)
Upvotes: 1
Views: 478
Reputation: 23
I've just figured it out, the Widget is now like this (changed the onPressed):
var formataData = new DateFormat('y/MM/dd');
var data = formataData.format(new DateTime.now());
final _funcao = funcoesBanco();
final docID = snapshot.data[index].reference.documentID;
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25)),
child: Text(
"Registrar", style: TextStyle(fontSize: 17),),
_funcao.atualizaCampos(docID,{
'saida2': data
});
),
And the function is like this:
atualizaCampos(document, novoCampo){
_firestore.collection('horarios').document(document).updateData(novoCampo);
}
Upvotes: 1