返回类型'对象' Isn; ta; ta; ta; widget; widget;

发布于 2025-02-13 19:34:48 字数 1450 浏览 0 评论 0原文

我尝试使用list.generate构建,但它们有错误的返回类型“对象”不是“窗口小部件”,这是封闭情况下的要求。但是,当我使用listView.builder时,我的应用程序正常工作。

Here my code
    List.generate(
                        data.length,
                        (index) {
                          return GestureDetector(
                            child: Padding(
                              padding: const EdgeInsets.only(
                                  bottom: 20, left: 20, right: 20),
                              child: CustomPromotionNew(
                                thumbNail: (snapshot.data as List)[index].imgUrl,
                                title:
                                    (snapshot.data as List)[index].promotionName,
                              ),
                            ),
                            onTap: () {
                              Navigator.push(
                                  context,
                                  MaterialPageRoute(
                                    builder: (context) =>
                                        PromotionNewsDetailScreen(
                                            items:
                                                (snapshot.data as List)[index]),
                                  ));
                            },
                          );
                        },)
 : const Center(
                          child: CircularProgressIndicator(),
                        );

I try to build with List.generate but they has error The return type 'Object' isn't a 'Widget', as required by the closure's context. But when i use ListView.builder, my application work fine.

Here my code
    List.generate(
                        data.length,
                        (index) {
                          return GestureDetector(
                            child: Padding(
                              padding: const EdgeInsets.only(
                                  bottom: 20, left: 20, right: 20),
                              child: CustomPromotionNew(
                                thumbNail: (snapshot.data as List)[index].imgUrl,
                                title:
                                    (snapshot.data as List)[index].promotionName,
                              ),
                            ),
                            onTap: () {
                              Navigator.push(
                                  context,
                                  MaterialPageRoute(
                                    builder: (context) =>
                                        PromotionNewsDetailScreen(
                                            items:
                                                (snapshot.data as List)[index]),
                                  ));
                            },
                          );
                        },)
 : const Center(
                          child: CircularProgressIndicator(),
                        );

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

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

发布评论

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

评论(1

情话难免假 2025-02-20 19:34:48

您会遇到此错误,因为您已定义了列表中的widget树类型。而不是您需要定义ListView。为了解决此错误,您需要在代码下方放置

ListView.builder(
        itemCount: data.length,
          itemBuilder: (context, i) {
            return GestureDetector(
              child: Padding(
                padding: const EdgeInsets.only(
                    bottom: 20, left: 20, right: 20),
                child: CustomPromotionNew(
                  thumbNail: (snapshot.data as List)[index].imgUrl,
                  title:
                  (snapshot.data as List)[index].promotionName,
                ),
              ),
              onTap: () {
                Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) =>
                          PromotionNewsDetailScreen(
                              items:
                              (snapshot.data as List)[index]),
                    ));
              },
            );
          },)
            : const Center(
        child: CircularProgressIndicator(),
    )

You getting this error because you have defined list type in widget tree. instead of that you need to define listview. For solve this error you need to put below code

ListView.builder(
        itemCount: data.length,
          itemBuilder: (context, i) {
            return GestureDetector(
              child: Padding(
                padding: const EdgeInsets.only(
                    bottom: 20, left: 20, right: 20),
                child: CustomPromotionNew(
                  thumbNail: (snapshot.data as List)[index].imgUrl,
                  title:
                  (snapshot.data as List)[index].promotionName,
                ),
              ),
              onTap: () {
                Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) =>
                          PromotionNewsDetailScreen(
                              items:
                              (snapshot.data as List)[index]),
                    ));
              },
            );
          },)
            : const Center(
        child: CircularProgressIndicator(),
    )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文