【问题标题】:NoSuchMethodError:The getter 'document' was called on null?NoSuchMethodError:在 null 上调用了 getter 'document'?
【发布时间】:2020-12-26 18:00:17
【问题描述】:

**你好我在我的代码中使用颤振和查询快照,但我不明白什么错误任何人帮助我如何在我的代码下面修复这个错误...... ..................................................... ..................................................... ..................................................... .... **

dd.dart

class SearchPage extends StatefulWidget {
  @override
  _SearchPageState createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage>
    with AutomaticKeepAliveClientMixin<SearchPage> {
  TextEditingController searchTextEditingController = TextEditingController();
  Future<QuerySnapshot> futureSearchResults;
  emptyTheTextFormField() {
    searchTextEditingController.clear();
  }

  controllSearching(String str) {
    Future<QuerySnapshot> allUsers = userRefrence
        .where("profileName", isGreaterThanOrEqualTo: str)
        .getDocuments();
    setState(() {
      futureSearchResults = allUsers;
    });
  }

  Container displayNoSearchResult() {
    final Orientation orientation = MediaQuery.of(context).orientation;
    return Container(
      child: Center(
        child: ListView(
          shrinkWrap: true,
          children: <Widget>[
            Icon(
              Icons.group,
              color: Colors.grey,
              size: 200.0,
            ),
            Icon(
              Icons.search,
              color: Colors.grey,
              size: 20.0,
            ),
            Text(
              "Search Users",
              textAlign: TextAlign.center,
              style: TextStyle(
                  color: Colors.white,
                  fontWeight: FontWeight.w500,
                  fontSize: 65.0),
            ),
          ],
        ),
      ),
    );
  }

  displayUsersFoundScreen() {
    return FutureBuilder(
      future: futureSearchResults,
      builder: (context, dataSnapshot) {
        if (dataSnapshot.hasData) {
          return circularProgress();
        }
        List<UserResult> searchUserResult = [];
        dataSnapshot.data.document.forEach((document) {
          User eachuser = User.fromDocument(document);
          UserResult userResult = UserResult(eachuser);
          searchUserResult.add(userResult);
        });
        return ListView(children: searchUserResult);
      },
    );
  }

  AppBar searchPageHeader() {
    return AppBar(
      backgroundColor: Colors.black,
      title: TextFormField(
        style: TextStyle(fontSize: 18.0, color: Colors.white),
        controller: searchTextEditingController,
        decoration: InputDecoration(
          hintText: "Search Here....",
          hintStyle: TextStyle(color: Colors.grey),
          enabledBorder: UnderlineInputBorder(
            borderSide: BorderSide(color: Colors.grey),
          ),
          focusedBorder: UnderlineInputBorder(
            borderSide: BorderSide(color: Colors.white),
          ),
          filled: true,
          prefixIcon: Icon(Icons.person_pin, color: Colors.white, size: 30.0),
          suffixIcon: IconButton(
              icon: Icon(
                Icons.clear,
                color: Colors.white,
              ),
              onPressed: emptyTheTextFormField),
        ),
        onFieldSubmitted: controllSearching,
      ),
    );
  }

  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      appBar: searchPageHeader(),
      body: futureSearchResults == null
          ? displayNoSearchResult()
          : displayUsersFoundScreen(),
    );
  }
}

class UserResult extends StatelessWidget {
  final User eachuser;

  UserResult(this.eachuser);

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(3.0),
      child: Container(
          color: Colors.white54,
          child: Column(
            children: <Widget>[
              GestureDetector(
                onTap: () => print("tapped"),
                child: ListTile(
                  leading: CircleAvatar(
                    backgroundColor: Colors.black,
                    backgroundImage: CachedNetworkImageProvider(eachuser.url),
                  ),
                  title: Text(
                    eachuser.profileName,
                    style: TextStyle(
                        color: Colors.black,
                        fontSize: 16.0,
                        fontWeight: FontWeight.bold),
                  ),
                  subtitle: Text(
                    eachuser.username,
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 13.0,
                    ),
                  ),
                ),
              )
            ],
          )),
    );
  }
}

【问题讨论】:

    标签: android flutter google-cloud-firestore


    【解决方案1】:

    由于您的if 条件,您收到错误,它应该是if (!dataSnapshot.hasData) {...}

    替换:

    if (dataSnapshot.hasData) {...}

    if (!dataSnapshot.hasData){...}.

    我使用您的代码添加了修复:

    displayUsersFoundScreen() {
        return FutureBuilder(
          future: futureSearchResults,
          builder: (context, dataSnapshot) {
            if (!dataSnapshot.hasData) { // should be !dataSnapshot.hasData
              return circularProgress();
            }
            List<UserResult> searchUserResult = [];
            dataSnapshot.data.document.forEach((document) {
              User eachuser = User.fromDocument(document);
              UserResult userResult = UserResult(eachuser);
              searchUserResult.add(userResult);
            });
            return ListView(children: searchUserResult);
          },
        );
      }
    

    【讨论】:

    • 它工作但发生 nosuchmethodError:class 'QuerySnapshot' has no instance getter 'document'
    • 请不要编辑您的原始帖子以提出另一个问题,而是单独创建另一个问题。
    • 你能告诉我解决办法评论问题吗
    猜你喜欢
    • 2021-07-18
    • 2018-12-03
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 2020-08-11
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多