Home > Net >  The return type 'Object' isn't a 'Widget', as required by the closure'
The return type 'Object' isn't a 'Widget', as required by the closure'

Time:07-07

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

CodePudding user response:

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(),
    )
  • Related