如何修复错误&quot“参数类型”对象?'可以分配给参数类型吗?

发布于 2025-02-09 08:22:48 字数 1653 浏览 1 评论 0原文

我正在尝试从Firebase存储中检索图像。我正在遵循YouTube教程,错误是在 snapshot.data 上说参数类型“对象?”无法分配到参数类型“小部件?”

请帮助我解决此错误。太感谢了。


final String image1 = "images/image0.png";
final String image2 = "images/image0.png";

String image = image1;

class LoadFirebaseStorageImage extends StatefulWidget {
  const LoadFirebaseStorageImage({Key? key}) : super(key: key); ```

            Container(
              height: double.infinity,
              margin: const EdgeInsets.only(left: 30, right: 30, top: 10),
              child: ClipRRect(
                borderRadius: BorderRadius.circular(30),
                child: FutureBuilder(
                  future: _getImage(context, image),
                  builder: (context, snapshot) {
                    if (snapshot.connectionState == ConnectionState.done)
                      return Container(
                          height: MediaQuery.of(context).size.height / 1.25,
                          width: MediaQuery.of(context).size.width / 1.25,
                          child: snapshot.data);
                    if (snapshot.connectionState == ConnectionState.waiting)
                      return Container(
                          height: MediaQuery.of(context).size.height / 1.25,
                          width: MediaQuery.of(context).size.width / 1.25,
                          child: CircularProgressIndicator());
                    return Container();
                  },
                ),
              ),
            ),
          ],
        ),
      ),
      loadButton(context),
    ],
  ),
);

} }``````

I am trying to retrieve an image from firebase storage. I was following a YouTube tutorial and the error was on the snapshot.data saying The argument type 'Object?' can't be assigned to the parameter type 'Widget?'.

Please help me to fix this error. Thank you so much.


final String image1 = "images/image0.png";
final String image2 = "images/image0.png";

String image = image1;

class LoadFirebaseStorageImage extends StatefulWidget {
  const LoadFirebaseStorageImage({Key? key}) : super(key: key); ```

            Container(
              height: double.infinity,
              margin: const EdgeInsets.only(left: 30, right: 30, top: 10),
              child: ClipRRect(
                borderRadius: BorderRadius.circular(30),
                child: FutureBuilder(
                  future: _getImage(context, image),
                  builder: (context, snapshot) {
                    if (snapshot.connectionState == ConnectionState.done)
                      return Container(
                          height: MediaQuery.of(context).size.height / 1.25,
                          width: MediaQuery.of(context).size.width / 1.25,
                          child: snapshot.data);
                    if (snapshot.connectionState == ConnectionState.waiting)
                      return Container(
                          height: MediaQuery.of(context).size.height / 1.25,
                          width: MediaQuery.of(context).size.width / 1.25,
                          child: CircularProgressIndicator());
                    return Container();
                  },
                ),
              ),
            ),
          ],
        ),
      ),
      loadButton(context),
    ],
  ),
);

}
}```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南…巷孤猫 2025-02-16 08:22:48

您需要在FutureBuilder上指示快照的类型,例如

FutureBuilder<Widget>(

假设_getImage返回widget

you need to indicate the type of the snapshot at the FutureBuilder, like

FutureBuilder<Widget>(

This is assuming that _getImage returns a Widget

拥抱没勇气 2025-02-16 08:22:48

您会遇到此错误,因为您已将snapshot.data直接解析到小部件。您无法将数据直接传递给小部件。
首先,请检查您在snapshot.data printing it上获得什么。
如果要获取图像的网络URL,则可以像以下内容一样使用它。

您将始终在snapshot.data中获取一些数据,而不是widget
只需检查您获得的打印功能即可。

       Container(
          height: double.infinity,
          margin: const EdgeInsets.only(left: 30, right: 30, top: 10),
          child: ClipRRect(
            borderRadius: BorderRadius.circular(30),
            child: FutureBuilder(
              future: _getImage(context, image),
              builder: (context, snapshot) {
                print(snapshot.data);

                if (snapshot.connectionState == ConnectionState.done)
                  return Container(
                      height: MediaQuery.of(context).size.height / 1.25,
                      width: MediaQuery.of(context).size.width / 1.25,
                      child: Image.network(snapshot.data));
                if (snapshot.connectionState == ConnectionState.waiting)
                  return Container(
                      height: MediaQuery.of(context).size.height / 1.25,
                      width: MediaQuery.of(context).size.width / 1.25,
                      child: CircularProgressIndicator());
                return Container();
              },
            ),
          ),
        ),
      ],
    ),
  ),

You are getting this error because you have parsed snapshot.data directly to the widget. You can not pass the data directly to the widget.
first, check what are you getting on snapshot.data by printing it.
if you are getting the network URL of the image then you can use it like below.

you will always get some data not widget in snapshot.data.
just check what are you getting print function.

       Container(
          height: double.infinity,
          margin: const EdgeInsets.only(left: 30, right: 30, top: 10),
          child: ClipRRect(
            borderRadius: BorderRadius.circular(30),
            child: FutureBuilder(
              future: _getImage(context, image),
              builder: (context, snapshot) {
                print(snapshot.data);

                if (snapshot.connectionState == ConnectionState.done)
                  return Container(
                      height: MediaQuery.of(context).size.height / 1.25,
                      width: MediaQuery.of(context).size.width / 1.25,
                      child: Image.network(snapshot.data));
                if (snapshot.connectionState == ConnectionState.waiting)
                  return Container(
                      height: MediaQuery.of(context).size.height / 1.25,
                      width: MediaQuery.of(context).size.width / 1.25,
                      child: CircularProgressIndicator());
                return Container();
              },
            ),
          ),
        ),
      ],
    ),
  ),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文