Firestore.instance.collection(collectio_name).document(document_name).get(), 在 Flutter 中没有从 firebase 获取任何值

Firestore.instance.collection(collectio_name).document(document_name).get(), does not get any value from firebase in Flutter

所以我在这里尝试使用 DocumentSnapshot 从 firebase 获取数据。 我想打印 document['display_name']

的值

输出:'Error has Occured' 在屏幕上

     class _HomeViewState extends State<HomeView> {
         Future<DocumentSnapshot> getDocument() async {
             return Firestore.instance
            .collection('user_data')
            .document('3vIf92LIJQ7pu7MpUwH1')
            .get();
          }

     @override
        Widget build(BuildContext context) {

           return Container(
           child: Center(
           child: FutureBuilder(
           future: getDocument(),
              builder: (BuildContext context, AsyncSnapshot snapshot) {
      if (snapshot.hasError) return Text('Error has occured');
      if (snapshot.connectionState == ConnectionState.waiting) {
        return CircularProgressIndicator();
      }
      if (snapshot.hasData) {
        return Column(
          children: <Widget>[
            Text(snapshot.data['display_name']),
          ],
        );
      }

如果 hasErrortrue,您需要打印 snapshot.error 以查看实际问题是什么:

if (snapshot.hasError) return Text('Error has occurred: ${snapshot.error}');

您的 Firestore 安全规则可能有误。此外,您需要访问 snapshot.data.data['display_name'] 而不是 snapshot.data['display_name'] 以从 firestore 获取值。

 if (snapshot.hasData) {
    return Column(
      children: <Widget>[
        Text(snapshot.data.data['display_name']),
      ],
    );
  }