在 null 上调用了方法“[]”。接收者:null 尝试调用:[]("subcategory")

The method '[]' was called on null. Receiver: null Tried calling: []("subcategory")

我正在尝试使用 flutter 和 futurebuilder 从 firebase 获取数据

这是代码

FutureBuilder<DocumentSnapshot>(
          future: _services.category.doc(_provider.selectedCategory).get(),
          builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot){
            if(snapshot.hasError){
              return Text('something went wrong...');
            }
            if(snapshot.connectionState == ConnectionState.waiting){
              return Center(child: CircularProgressIndicator(),);
            }
            Map<String, dynamic>data = snapshot.data.data();
            return Expanded(
              child: Column(
                children: [
                  Padding(
                    padding: const EdgeInsets.all(10),
                    child: Container(
                      child: Row(

                        children: [
                          Text('Main Category:   '),
                          FittedBox(
                              child: Text(_provider.selectedCategory,
                                style: TextStyle(fontWeight: FontWeight.bold),)),
                        ],
                      ),
                    ),
                  ),
                  Divider(thickness: 3,),
                  Container(
                    child: Expanded(
                      child: Padding(
                        padding: const EdgeInsets.all(10),
                        child: ListView.builder(
                          itemCount: data['subcategory'] == null ? 0: data['subcategory'].length,
                            itemBuilder: (BuildContext context, int index){
                              return ListTile(
                                contentPadding: EdgeInsets.zero,
                                leading: CircleAvatar(
                                  child: Text('${index + 1}'),
                                ),
                                title: Text(data['subcategory'][index]['name']),
                                onTap: (){
                                  _provider.selectSubCategory(data['subcategory'][index]['name']);
                                  Navigator.pop(context);
                                },

_provider.selectedCategory 来自另一个流构建器,用户根据同样来自 firebase 数据的选项选择类别

_服务=CollectionReference category = FirebaseFirestore.instance.collection('categories');

但是文档快照好像是空的

谁能帮我解决这个问题?? 谢谢

尝试添加 if (snapshot.connectionState == ConnectionState.done&& snapshot.hasData && snapshot.data != null){return your widget; }