【问题标题】:Exception: Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist异常:错误状态:无法获取 DocumentSnapshotPlatform 上不存在的字段
【发布时间】:2021-05-16 08:15:55
【问题描述】:

此线程https://stackoverflow.com/a/50867881/13153574 中提到的方法我正在尝试从 Firestore 获取数据。但得到以下异常。 'name' 字段是一个字符串,'overview' 字段是一个字符串列表。

Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist

我的代码如下:

import 'package:firebaseAuth/firebaseAuthDemo.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

class FindDiseases extends StatefulWidget {
  final User user;

  const FindDiseases({Key key, this.user}) : super(key: key);
  @override
  _FindDiseasesState createState() => _FindDiseasesState();
}

class _FindDiseasesState extends State<FindDiseases> {
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

  FirebaseAuth _auth = FirebaseAuth.instance;

  List diseasesList = [];
  //dynamic data;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.teal,
        automaticallyImplyLeading: false,
        title: Text(
          "Diseases List",
        ),
      ),
      key: _scaffoldKey,
      body: Center(
        child: FlatButton(
          color: Colors.white,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Text("get Disease Record"),
              StreamBuilder<DiseaseRecord>(
                stream: getDisease(),
                builder: (BuildContext c, AsyncSnapshot<DiseaseRecord> data) {
                  if (data?.data == null) return Text("Error");

                  DiseaseRecord r = data.data;

                  return Text("${r.name}");
                },
              ),
            ],
          ),
          onPressed: () {
            getDisease();
          },
        ),
      ),
    );
  }

  Future _signOut() async {
    await _auth.signOut();
  }
}

Stream<DiseaseRecord> getDisease() {
  return FirebaseFirestore.instance.collection("diseases").doc().get().then(
    (snapshot) {
      try {
        return DiseaseRecord.fromSnapshot(snapshot);
      } catch (e) {
        print(">>> Error:"+e.toString());
        return null;
      }
    },
  ).asStream();
}

class DiseaseRecord {

  String name;
  List<String> overview = new List<String>();

  DiseaseRecord.fromSnapshot(DocumentSnapshot snapshot)
      : name = snapshot['name'],
        overview = List.from(snapshot['overview']);
}

数据如下:

name: "name--"
overview: "['a', 'b', 'c']"

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    问题出在这里:

    return FirebaseFirestore.instance.collection("diseases").doc().get()
    

    在不带任何参数的情况下调用doc() 会创建对一个新的、不存在的文档的引用。然后在其上调用get(),为不存在的文档返回DocumentSnapshot,尝试从中获取字段是无效操作。

    您很可能需要知道您尝试加载的疾病文档的 ID,并将其传递给doc(id) 的调用。

    【讨论】:

    • 但是我在'diseases' 收藏中有很多文档。所以我怎么能有人..?我也试过了,但它给了type 'String' is not a subtype of type 'Iterable&lt;dynamic&gt;' 异常。 :(
    • 在询问如何和后续问题之前:阅读我上面的解释后,您的代码不起作用的原因是否有意义?
    猜你喜欢
    • 1970-01-01
    • 2023-01-11
    • 2021-10-26
    • 2021-06-17
    • 2021-05-19
    • 2021-04-27
    • 1970-01-01
    • 2021-03-20
    • 2021-08-15
    相关资源
    最近更新 更多