当悬空时,如何在Firestore系列中添加新字段



我正在使用Flutter和Google Firestore,我需要更新一个集合(添加新字段(,当离线时,我正在使用飞机模式进行测试。当我单击按钮时,应用程序崩溃,数据未发送到数据库。它在线工作很好。

我认为这发生了,因为异步/等待着,但是如果我不使用它们,则该应用在线时甚至无法保存。

widget with是调用更新函数的函数(我正在使用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)

我刚刚弄清楚了,小部件现在就是这样(更改了打开的(:

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);
}

相关内容

  • 没有找到相关文章

最新更新