【问题标题】:The getter 'documents' was called on null shown for a whilegetter 'documents' 在 null 上被调用了一段时间
【发布时间】:2020-09-23 16:35:54
【问题描述】:

我正在尝试从 appBar 上的 firebase 检索用户名。它成功地检索它。但是在成功显示用户名之前,它会在屏幕上显示几秒钟的错误。错误操作系统

I/flutter (24143):在构建时抛出了以下 NoSuchMethodError StreamBuilder(脏,状态:

I/flutter (24143): _StreamBuilderBaseState#c10cf):

I/flutter (24143):getter 'documents' 在 null 上被调用。

I/flutter (24143):接收器:null

I/flutter (24143):尝试调用:文档

Class Data{    
    Widget build(BuildContext context) {
    
        SystemChrome.setPreferredOrientations([
          DeviceOrientation.portraitUp,
        ]);
        CurrentUser _currentUser = Provider.of<CurrentUser>(context, listen: false);
        return Scaffold(
          backgroundColor: Colors.grey[600],
          resizeToAvoidBottomPadding: false,
          appBar: AppBar(
            title: Text('Property Host'),
            centerTitle: true,
            actions: <Widget>[
              Expanded(
    
                child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: <Widget>[
                    Container(
                        margin: new EdgeInsets.only(left: 50),
                        child: Text('Property Host',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 19),)),
                    StreamBuilder(stream: Firestore.instance.collection('users').where("uid", isEqualTo: userid).snapshots(),
    
        // ignore: missing_return
        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
          if (snapshot.data == null)
            CircularProgressIndicator();
            //final userDocument = snapshot.data;
            //final title=  snapshot.data.userocument['displayName']);
            //CircularProgressIndicator();
            return Expanded(
              child: ListView.builder(
                  itemCount: snapshot.data.documents.length,
                  // ignore: missing_return
                  itemBuilder: (BuildContext context, int index) {
                    print(user.uid);
                    return user != null
                        ? Container(
                      margin: EdgeInsets.only(top: 17, left: 40),
                      child: Text(
                          snapshot.data.documents.elementAt(index)['displayName']),
                    )
                        : IconButton(
                      icon: Icon(Icons.person),
                      // ignore: missing_return
                      onPressed: () {
                        Navigator.pushNamed(context, '/LoginScreen');
                      },
                    );
                  }
              ),
            );

}

【问题讨论】:

  • 你忘了return之前的CircularProgressIndicator();

标签: firebase flutter google-cloud-firestore


【解决方案1】:

使用 snapshot.hasData 确保您在检索数据后构建主窗口小部件(扩展),并使用 CircularProgessIndicator 在返回数据之前保持其位置

 builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
              if (snapshot.hasData){
                return Expanded(
                  child: ListView.builder(
                      itemCount: snapshot.data.documents.length,
                      // ignore: missing_return
                      itemBuilder: (BuildContext context, int index) {
                        print(user.uid);
                        return user != null
                            ? Container(
                          margin: EdgeInsets.only(top: 17, left: 40),
                          child: Text(
                              snapshot.data.documents.elementAt(index)['displayName']),
                        )
                            : IconButton(
                          icon: Icon(Icons.person),
                          // ignore: missing_return
                          onPressed: () {
                            Navigator.pushNamed(context, '/LoginScreen');
                          },
                        );
                      }
                  ),
                );
                }
                 else {
                   return CircularProgressIndicator();
                   }
    
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 2020-01-10
    • 2020-10-24
    • 2021-04-13
    • 2021-10-22
    相关资源
    最近更新 更多