Home > Mobile >  Add an extra page in the end of list for pageview.builder
Add an extra page in the end of list for pageview.builder

Time:08-22

I try to add an extra page in the end of list for pageview.builder, below is the code I try to achieve this effect, but in the end I got the error :

RangeError (index): Invalid value: Not in inclusive range 0..1: 2

here is the code:

 PageView.builder(
                  physics: BouncingScrollPhysics(),
                  controller: _pageController,
                  itemCount: controller.homeProfiles.length   1,
                  itemBuilder: (context, index) {

                    final Profile profile = controller.homeProfiles[index];
                   
                    //below I try to add an extra page in the end of list
                        return index <= controller.homeProfiles.length
                            ? ProfileWidget(
                                profile: profile,
                              )
                            : AddExtraPage();
                      },
                    ),

What shall I do to achieve always add an extraPage in the end of this pageview? thanks a lot!

CodePudding user response:

It should be less than < instead less than or equal <= operator. Also, controller.homeProfiles[index] should be inside the ternary if. Like the following snippet:

return index < controller.homeProfiles.length
    ? ProfileWidget(
        profile: controller.homeProfiles[index],
      )
    : AddExtraPage();

CodePudding user response:

try using this it will solve your issue. In below firstly you will check if index == length then you will show your ExtraPage otherwise your ProfileWidget.

  return index == controller.homeProfiles.length
                  ? AddExtraPage()
                  : ProfileWidget(profile: profile);
  • Related