我正在使用flutter和Google Firestore,并且需要更新集合(添加新字段),离线时,我正在使用飞行模式进行测试。当我单击按钮时,应用程序崩溃,并且数据未发送到数据库。可以在网上正常运行。
我认为这是由于异步/等待而发生的,但是,如果我不使用它们,则该应用程序甚至在联机时也无法保存。
小部件,其中的是调用更新功能的功能(我正在使用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]);
},
),
更新功能:
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,
});
});
}
预期结果是即使已脱机也将数据添加到Firestore中。
我在控制台中收到的错误:
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)
答案 0 :(得分:1)
我已经弄清楚了,小部件现在是这样的(更改了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
});
),
函数是这样的:
atualizaCampos(document, novoCampo){
_firestore.collection('horarios').document(document).updateData(novoCampo);
}