为什么在这种情况下我会收到错误状态错误?

时间:2021-06-18 22:27:32

标签: firebase flutter google-cloud-firestore

我将所有软件包更新到最新版本,现在我收到此错误:获取某些数据时出现 Bad state: field does not exist within the DocumentSnapshotPlatform

我在这里发现了一些与同一问题相关的问题,但没有一个能解决我的问题,其中大部分与 DocumentSnapshot 工作方式的更改有关,我可能一定错过了导致此错误的文档中的某些内容。

好吧,我会在下面列出我认为有问题的地方以及完整的代码。

我认为问题出在这里

List<UserResult> searchResults = [];
 snapshot.data.docs.forEach((doc) {
  Users user = Users.fromDocument(doc);
  UserResult searchResult = UserResult(user);
  searchResults.add(searchResult);
});

我尝试过但没有奏效的事情

1:

List<UserResult> searchResults = [];
  snapshot.data().docs.forEach((doc) {
  Users user = Users.fromDocument(doc);
  UserResult searchResult = UserResult(user);
  searchResults.add(searchResult);
 });

2:

List<UserResult> searchResults = [];
  var item = snapshot.data();
   item.docs.forEach((doc) {
   Users user = Users.fromDocument(doc);
   UserResult searchResult = UserResult(user);
   searchResults.add(searchResult);
 });

为了理解

class _SearchState extends State<Search> {
  TextEditingController searchController = TextEditingController();
  Future<QuerySnapshot> searchResultsFuture;

  handleSearch(String query) {
      Future<QuerySnapshot> results =
          usersRef.where("tag", isEqualTo: query).get();

      setState(() {
        searchResultsFuture = results;
      });
  }

  buildResults() {
    return FutureBuilder(
      future: searchResultsFuture,
      builder: (context, snapshot) {
        if (snapshot.connectionState != ConnectionState.done) {
          return SizedBox(
              child: CircularProgressIndicator(),
            );
        }
        List<UserResult> searchResults = [];
        snapshot.data.docs.forEach((doc) {
          Users user = Users.fromDocument(doc);
          UserResult searchResult = UserResult(user);

          searchResults.add(searchResult);
        });
        return SingleChildScrollView(
          child: Column(
            children: searchResults,
          ),
        );
      },
    );
  }

  buildNoContent() {
    return Container();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
            child: SingleChildScrollView(
              child: Column(
                children: <Widget>[
                  Center(
                    child: Text(
                        "Type something",
                      ),
                    ),
                  TextFormField(
                    enableInteractiveSelection: true,
                    controller: searchController,
                    keyboardType: TextInputType.text,
                    onFieldSubmitted: handleSearch(searchController.text),
                  ),
                  
                  searchResultsFuture == null
                      ? buildNoContent()
                      : buildResults(),
                ],
              ),
            ),
      ),
    );
  }
}

fromDocument 方法

import 'package:cloud_firestore/cloud_firestore.dart';

class Users {
  String id;
  String email;
  String tag;

  Users(
      {this.id,
      this.email,
      this.tag});

  factory Users.fromDocument(DocumentSnapshot doc) {
    return Users(
        id: doc.id,
        email: doc['email'],
        tag: doc['tag']);
  }
}

2 个答案:

答案 0 :(得分:2)

改变

FutureBuilder(

FutureBuilder<QuerySnapshot>(

在你的工厂方法中,这样做:

factory Users.fromDocument(DocumentSnapshot doc) {
    return Users(
        id: doc.id,
        email: doc.data()['email'],
        tag: doc.data()['tag']);
  }

答案 1 :(得分:1)

在尝试了大量组合后,这奏效了:

fromDocument 方法

import 'package:cloud_firestore/cloud_firestore.dart';

class Users {
  String id;
  String email;
  String tag;

  Users(
      {this.id,
      this.email,
      this.tag});

  factory Users.fromDocument(DocumentSnapshot<Map<String, dynamic>> doc) {
    return Users(
        id: doc.id,
        email: doc.data()['email'],
        tag: doc.data()['tag']);
  }
}

FutureBuilder

class _SearchState extends State<Search> {
  TextEditingController searchController = TextEditingController();
  Future<QuerySnapshot<Map<String, dynamic>>> searchResultsFuture;

  handleSearch(String query) {
      Future<QuerySnapshot> results =
          usersRef.where("tag", isEqualTo: query).get();

      setState(() {
        searchResultsFuture = results;
      });
  }

  buildResults() {
    return FutureBuilder<QuerySnapshot<Map<String, dynamic>>>(
      future: searchResultsFuture,
      builder: (context, snapshot) {
        if (snapshot.connectionState != ConnectionState.done) {
          return SizedBox(
              child: CircularProgressIndicator(),
            );
        }
        List<UserResult> searchResults = [];
        snapshot.data.docs.forEach((doc) {
          Users user = Users.fromDocument(doc);
          UserResult searchResult = UserResult(user);

          searchResults.add(searchResult);
        });
        return SingleChildScrollView(
          child: Column(
            children: searchResults,
          ),
        );
      },
    );
  }

基本上我只是添加了 >