【问题标题】:Get information from a specific Firestore document for its id in Flutter从特定 Firestore 文档中获取其在 Flutter 中的 id 信息
【发布时间】:2019-06-08 06:44:38
【问题描述】:

我正在尝试通过其“id”获取特定文档的信息以显示该文档的信息。

我用于服务的代码是这样的:

class SentenciasFireBase {
  //This is a class where I have all the services of firebase
  ...
  obtenerVacaciones(String empleado) async {
    //empleado is the document id
    return await Firestore.instance.document('Vacaciones/$empleado');
  }
  ...
}

显示信息的代码是这样的:

class TurnoActual_State extends State<TurnoActual>{
  ...  
  SentenciasFireBase objetoSentencias = new SentenciasFireBase();
  //This object "metodos" is of a class that has several reusable methods:
  Metodos metodos = new Metodos();

  @override
  void initState() {
    objetoSentencias.obtenerVacaciones(_noEmpleado).then((resultados){
      setState(() {
        vacaciones = resultados;
      });
    });
  }
  ...
  @override
  Widget build(BuildContext context){
  ...
  RichText(
    text: TextSpan(
      //toDateTime is a method that converts a timestamp into a DateTime, 
      //and then converts the DateTime into a String with a specific date format.
      //'Inicio' stores a timestamp (it is the date on which the holidays start):
      text: metodos.toDateTime(vacaciones.documents[0].data['Inicio']),
      style: TextStyle(
        color: Colors.black,
        fontSize: 30,
        fontWeight: FontWeight.bold,
      ),
    )
  )
  ...
}

这是错误信息:
颤振:══╡ 小部件库发现的异常。 ╞═════════════════════════════════════════════════ ══════════
颤振:在构建 StreamBuilder(脏,状态:
颤振:_StreamBuilderBaseState>#45dbd):
颤振:在 null 上调用了 getter 'documents'
颤振:接收者:空
颤振:尝试调用:文档

注意:
我把文档的引用放错了,不是“Turnos”:

return await Firestore.instance.document('Turnos/$empleado');

但是“Vacaciones”:

return await Firestore.instance.document('Vacaciones/$empleado');

(我在问题中更新了它)但无论如何它不会“影响”问题本身。

【问题讨论】:

    标签: flutter google-cloud-firestore


    【解决方案1】:

    您提到了集合名称来访问特定的文档记录,那么如果您有多个记录,您可以使用 forEach 从每个迭代记录中检索 docuemntId。

    在您的情况下,只需尝试在文档之前添加集合名称。像下面的代码。

     Firestore.instance
        .collection('collection')
        .document(newCategory.field1)
        ....
    });
    

    【讨论】:

    • 您可以将整个路径传递给对document(...)的调用,因此在OP的代码(document('Turnos/$empleado'))中,有一个集合Turnos和一个文档$empleado
    【解决方案2】:

    由于您调用的是document(),因此结果将是一个文档。但是在您的 build 方法中,您正试图从集合中获取文档:

    text: metodos.toDateTime(vacaciones.documents[0].data['Inicio']),
    

    由于vacaciones 是单个文档,因此应该是这样的:

    text: metodos.toDateTime(vacaciones.data['Inicio']),
    

    【讨论】:

    • 什么类型的变量必须是“vacaciones”才能显示信息?在我的代码中,我将其定义为“QuerySnapshot”,如下所示:QuerySnapshot vacaciones; 当我想使用您向我提到的语法时,它标记我一个错误:“没有为类定义 getter 'data' '查询快照'"
    • 那就是DocumentSnapshot。对于这种情况,我强烈建议您将reference documentation 放在手边。
    • 我已经将“vacaciones”变量的类型更改为DocumentSnapshot,现在我收到了这个新错误:“getter 'data' was called on null”。我首先想到的是我从文档中获取信息的服务是错误的,但是如果我在流构建器中使用与它相同的文档引用:stream: Firestore.instance.document ('Vacaciones/$_noEmpleado'). Snapshots ( ),它允许我显示信息,这已经解决了我的问题,但即便如此,我还是想知道为什么会以其他方式出现该错误。
    猜你喜欢
    • 2022-07-22
    • 1970-01-01
    • 2020-12-05
    • 2021-07-20
    • 2021-10-22
    • 2021-12-16
    • 2021-10-03
    • 2019-05-05
    • 1970-01-01
    相关资源
    最近更新 更多