首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在null上调用了方法'[]‘。Receiver:空,尝试调用:[]("pictures")

在null上调用了方法'[]‘。Receiver:空,尝试调用:[]("pictures")
EN

Stack Overflow用户
提问于 2021-03-23 21:17:08
回答 1查看 79关注 0票数 0

你好吗?我需要我的代码.No的帮助,无论我如何寻找问题,我找不到原因。我使用futureBuilder从firebase收集数据,并将其显示在应用程序中。当我执行我说的代码时:方法'[]‘在null上被调用。接收者:空,已尝试调用:。

代码

代码语言:javascript
复制
Container(
                    child: FutureBuilder(
                      future: roomProduct.RoomData.doc().get(),
                      builder: (context, snap) {
                        if (snap.connectionState ==
                            ConnectionState.done) {
                          Map<String, dynamic> docs = snap.data.data();
                          return ListView(
                            shrinkWrap: true,
                            children: [
                              Padding(
                                padding: const EdgeInsets.all(6.0),
                                child: Container(
                                  decoration: BoxDecoration(
                                      color: Colors.white,
                                      borderRadius: BorderRadius.circular(10.0),
                                      boxShadow: [
                                        BoxShadow(
                                            color: Colors.grey,
                                            offset: Offset(-2,-1),
                                            blurRadius: 5
                                        )
                                      ]
                                  ),
                                  child: GestureDetector(

                                    child: Column(
                                      children: [
                                        Row(
                                          mainAxisAlignment: MainAxisAlignment.center,
                                          children: [
                                            ClipRRect(
                                                borderRadius: BorderRadius.circular(7.0),
                                                child: Image.network(

                                                  "${docs['pictures'][0]}",
                                                  height: 500,
                                                  width: 450,
                                                )
                                            ),
                                          ],
                                        ),
                                        Center(child: Text('${docs['name']} \n',style: 
                                       TextStyle(fontSize: 24.0,))),
                                        Center(child: Text('id:     ${docs['price']} \n',style: 
                                     TextStyle(fontSize: 24.0,))),
                                      ],
                                    ),
                                  ),
                                ),
                              )
                            ],
                          );
                        }
                        return Container();
                      },

                    ),
                  )

错误

代码语言:javascript
复制
The following NoSuchMethodError was thrown building FutureBuilder<DocumentSnapshot>(dirty, state: 
_FutureBuilderState<DocumentSnapshot>#59eb3):
 The method '[]' was called on null.
  Receiver: null
 Tried calling: []("pictures")

 The relevant error-causing widget was: 
   FutureBuilder<DocumentSnapshot> 
   
   When the exception was thrown, this was the stack: 
   #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
    #1      _IncorpoBidPageState.build.<anonymous closure>.<anonymous closure> 
   (package:MerchantIsland/bidPages/roomPage.dart:122:62)
   #2      _FutureBuilderState.build (package:flutter/src/widgets/async.dart:773:55)
   #3      StatefulElement.build (package:flutter/src/widgets/framework.dart:4612:27)
   #4      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4495:15)
   ...
 ====================================================================================================
  /DynamiteModule( 4330): Local module descriptor class for providerinstaller not found.
  I/DynamiteModule( 4330): Considering local module providerinstaller:0 and remote module 
   providerinstaller:0
   W/ProviderInstaller( 4330): Failed to load providerinstaller module: No acceptable module found. 
  Local version is 0 and remote version is 0.
  W/DynamiteModule( 4330): Local module descriptor class for providerinstaller not found.
  I/DynamiteModule( 4330): Considering local module providerinstaller:0 and remote module 
  providerinstaller:0
  W/ProviderInstaller( 4330): Failed to load providerinstaller module: No acceptable module found. 
  Local version is 0 and remote version is 0.
  I/FirebaseAuth( 4330): [FirebaseAuth:] Preparing to create service connection to fallback 
   implementation
   W/System  ( 4330): Ignoring header X-Firebase-Locale because its value was null.
EN

回答 1

Stack Overflow用户

发布于 2021-03-23 21:52:26

这是因为您正在尝试调用docs['pictures'],而您在这里发布的代码片段中没有声明docs。这就是为什么它会给出一个空值错误,因为该变量对于编译器来说是未知的,所以它将其视为空值。因此,您不能访问null变量的'pictures‘属性。

尝试预先声明docs变量,在使用它之前,只需执行null检查该值是否为null或其他值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66763954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档