【问题标题】:method '[]' was called on null [closed]方法 \'[]\' 在 null 上被调用 [关闭]
【发布时间】:2022-09-27 21:13:12
【问题描述】:

当我想打开这样说的产品页面时。

在构建 FutureBuilder(dirty, state: _FutureBuilderState#03644) 时引发了以下 NoSuchMethodError: 在 null 上调用了方法 \'[]\'。 接收方:空 尝试调用:

相关的导致错误的小部件是: FutureBuilder FutureBuilder:file:///E:/​​vscode/popkissofficial/lib/screens/product_page.dart:43:11

enter image description here

FutureBuilder(
            future: _firebaseServices.productsRef.doc(widget.productId).get(),
            builder: (context, snapshot) {
              if (snapshot.hasError) {
                return Scaffold(
                  body: Center(
                    child: Text(\"Error: ${snapshot.error}\"),
                  ),
                );
              }

              if (snapshot.connectionState == ConnectionState.done) {
                Map<String, dynamic> documentData = snapshot.data.data();

                List imageList = documentData[\'images\'];
                List productSizes = documentData[\'size\'];

                _selectedProductSize = productSizes[1];

                return ListView(
                  padding: EdgeInsets.all(0),
                  children: [
                    ImageSwipe(
                      imageList: imageList,
                    ),
                    Padding(
                      padding: const EdgeInsets.only(
                        top: 24.0,
                        left: 24.0,
                        right: 24.0,
                        bottom: 4.0,
                      ),
                      child: Text(
                        \"${documentData[\'name\']}\",
                        style: Constants.boldHeading,
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.symmetric(
                        vertical: 4.0,
                        horizontal: 24.0,
                      ),
                      child: Text(
                        \"\\Rp${documentData[\'price\']}\",
                        style: TextStyle(
                          fontSize: 18.0,
                          color: Theme.of(context).colorScheme.secondary,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.symmetric(
                        vertical: 8.0,
                        horizontal: 24.0,
                      ),
                      child: Text(
                        \"${documentData[\'desc\']}\",
                        style: const TextStyle(
                          fontSize: 16.0,
                        ),
                      ),
                    ),
                    const Padding(
                      padding: EdgeInsets.symmetric(
                        vertical: 24.0,
                        horizontal: 24.0,
                      ),
                      child: Text(
                        \"Select Size\",
                        style: Constants.regularDarkText,
                      ),
                    ),
                    ProductSize(
                      productSizes: productSizes,
                      onSelected: (size) {
                        _selectedProductSize = size;
                      },
                    ),
                    Padding(
                      padding: const EdgeInsets.all(24.0),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          GestureDetector(
                            onTap: () async {
                              await _addToSaved();
                              ScaffoldMessenger.of(context).showSnackBar(_snackBar);
                            },
                            child: Container(
                              width: 65.0,
                              height: 65.0,
                              decoration: BoxDecoration(
                                color: Color(0xFFDCDCDC),
                                borderRadius: BorderRadius.circular(12.0),
                              ),
                              alignment: Alignment.center,
                              child: const Image(
                                image: AssetImage(
                                  \"assets/images/tab_saved.png\",
                                ),
                                height: 22.0,
                              ),
                            ),
                          ),
                          Expanded(
                            child: GestureDetector(
                              onTap: () async {
                                await _addToCart();
                                ScaffoldMessenger.of(context).showSnackBar(_snackBar);
                              },
                              child: Container(
                                height: 65.0,
                                margin: EdgeInsets.only(
                                  left: 16.0,
                                ),
                                decoration: BoxDecoration(
                                  color: Colors.black,
                                  borderRadius: BorderRadius.circular(12.0),
                                ),
                                alignment: Alignment.center,
                                child: const Text(
                                  \"Add To Cart\",
                                  style: TextStyle(
                                      color: Colors.white,
                                      fontSize: 16.0,
                                      fontWeight: FontWeight.w600),
                                ),
                              ),
                            ),
                          )
                        ],
                      ),
                    )
                  ],
                );
              }

              return Scaffold(
                body: Center(
                  child: CircularProgressIndicator(),
                ),
              );
            },
          ),
          CustomActionBar(
            hasBackArrrow: true,
            hasTitle: false,
            hasBackground: false,
          )
  • 你能分享一下firebase对象图像吗?
  • 请粘贴您的应用程序的实际代码和错误(在您的问题中)而不是代码的图像。
  • 请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。
  • \"寻求调试帮助的问题 (\'为什么这段代码不起作用?\') 必须包含所需的行为,a具体问题或错误必要的最短代码重现它在问题本身.没有的问题明确的问题陈述对其他读者没有用。见:How to create a Minimal, Complete, and Verifiable example.\"

标签: flutter firebase


【解决方案1】:

您正在尝试获取数组中索引 0 上的对象,但如果数组为空怎么办?

你不是那样处理状态

【讨论】:

    猜你喜欢
    • 2021-02-26
    • 2021-08-22
    • 2021-11-30
    • 2020-11-10
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 2019-02-07
    相关资源
    最近更新 更多