Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The return type 'Object' isn't a 'Widget', as required by the closure's context

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(),
                        );

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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(),
    )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading