Steps to reproduce
Created enum for pageview pages. Created pageview controller and store in provider for accessing in to pageview's pages. When I trying to go next page in page 15 , pageview builder index reproduce value to 16 and after 0. You can see pageview index values in logs. When I trying to next page at page15 , item builder was print index 16 as correctly but after it that change it to 0. I cant see page 16.
Im using context.read().questionPageController in two pages explicitly, I even tried to remove controller from these pages and just used in QuestionView but there is no change. Other than that, I don't interfere with the controller object from anywhere else anyway.
When I opened a project from scratch and created an 18-page pageview just like mine, I didn't encounter a problem. Thank you.
Expected results
PageView itembuilder should give to item index 16
Actual results
PageView itembuilder giving item index 16 and 0
Code sample and log
// My Onboard Pages---------------------------------------------
enum QuestionPagesEnum {
questionStart,
gender,
heightWeightAge,
focusParts,
target,
desiredMetrics,
desiredMetricsGoal,
desiredMetricsWeightGoal,
targetBasedBenefit,
injury,
equipment,
wearable,
dayCountAvailable,
daysAvailable,
analyzingYourData,
fitTest,
fitnessLevel,
daysAvaiableAfterLoadPage,
}
extension XQuestionPagesEnum on QuestionPagesEnum {
Widget toPage() {
switch (this) {
case QuestionPagesEnum.questionStart:
return const QuestionStartPage();
case QuestionPagesEnum.gender:
return const QuestionGenderPage();
case QuestionPagesEnum.heightWeightAge:
return const HeightWeightAgePage();
case QuestionPagesEnum.focusParts:
return const FocusPartsPage();
case QuestionPagesEnum.target:
return const TargetPage();
case QuestionPagesEnum.desiredMetrics:
return const DesiredMetricsPage();
case QuestionPagesEnum.desiredMetricsGoal:
return const DesiredMetricsGoalPage();
case QuestionPagesEnum.desiredMetricsWeightGoal:
return const DesiredMetricsWeightGoalPage();
case QuestionPagesEnum.targetBasedBenefit:
return const TargetBasedBenefitPage();
case QuestionPagesEnum.fitnessLevel:
return const FitnessLevelPage();
case QuestionPagesEnum.injury:
return const InjuryPage();
case QuestionPagesEnum.equipment:
return const EquipmentPage();
case QuestionPagesEnum.wearable:
return const WearablePage();
case QuestionPagesEnum.analyzingYourData:
return const AnalyzingDataPage();
case QuestionPagesEnum.dayCountAvailable:
return const DayCountAvailablePage();
case QuestionPagesEnum.daysAvailable:
return const DaysAvailablePage();
case QuestionPagesEnum.daysAvaiableAfterLoadPage:
return const DaysAvaiableAfterLoadPage();
case QuestionPagesEnum.fitTest:
return const FitTestPage();
default:
return const QuestionStartPage();
}
}
}
// My Onboard View--------------------------------------------------------
class QuestionsView extends StatefulWidget {
const QuestionsView({super.key});
@override
State<QuestionsView> createState() => _QuestionsViewState();
}
class _QuestionsViewState extends State<QuestionsView> {
@override
Widget build(BuildContext context) {
ColorConstant.setDefaultUIOverlayStyle();
return Selector<OnBoardViewModel, int>(
selector: (p0, p1) => p1.questionsPageIndex,
shouldRebuild: (previous, next) => true,
builder: (context, index, _) {
return AppScaffold(
child: SafeArea(
child: Selector<OnBoardViewModel, bool>(
selector: (p0, p1) => p1.isRouteForCompleteOnboard,
builder: (context, isRouteForCompleteOnboard, _) {
return Column(
children: [
const Expanded(child: _PageView()),
Visibility(
visible: index == QuestionPagesEnum.daysAvailable.index,
child: _daysAvailableBottomText(context),
),
Visibility(
visible: isVisibleNextButton(index),
child: _nextPageButton(index),
),
if (index != QuestionPagesEnum.fitTest.index) ...{
SizedBox(height: MediaQuery.of(context).viewPadding.bottom == 0 ? 16 : 0),
},
],
);
}),
),
);
},
);
// Next Page Button
Widget _nextPageButton(int index) {
return Consumer<OnBoardViewModel>(
builder: (context, viewmodel, child) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: ButtonPrimary(
width: double.maxFinite,
onPressed: _isContinue(viewmodel)
? () async {
FocusManager.instance.primaryFocus?.unfocus();
if (viewmodel.heightWeightFormKey.currentState?.validate() == false) return;
if (viewmodel.desiredMetricsGoalWeightFormKey.currentState?.validate() == false) return;
viewmodel.questionsPageController!.nextPage(
duration: const Duration(milliseconds: 200),
curve: Curves.linear,
);
}
: null,
),
);
},
);
}
}
// My Page View--------------------------------------------------------
class _PageView extends StatefulWidget {
const _PageView();
@override
State<_PageView> createState() => _PageViewState();
}
class _PageViewState extends State<_PageView> {
final controller = PageController();
@override
void initState() {
super.initState();
context.read<OnBoardViewModel>().questionsPageController = controller;
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return PageView.builder(
controller: controller,
physics: const NeverScrollableScrollPhysics(),
itemCount: QuestionPagesEnum.values.length,
onPageChanged: (value) {
print('onPageChanged $value');
context.read<OnBoardViewModel>().setQuestionsPageIndex(value);
},
itemBuilder: (context, index) {
print('itemBuilder $index');
return QuestionPagesEnum.values[index].toPage();
});
}
}
### Logs
Launching lib\main.dart on sdk gphone64 x86 64 in debug mode...
√ Built build\app\outputs\flutter-apk\app-debug.apk.
D/FlutterGeolocator( 5437): Attaching Geolocator to activity
I/TextToSpeech( 5437): Connected to TTS engine
D/HostConnection( 5437): createUnique: call
D/HostConnection( 5437): HostConnection::get() New Host Connection established 0x72b870fa3ed0, tid 5607
D/HostConnection( 5437): HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_YUV_Cache ANDROID_EMU_has_shared_slots_host_memory_allocator ANDROID_EMU_sync_buffer_data ANDROID_EMU_read_color_buffer_dma GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_2
W/OpenGLRenderer( 5437): Failed to initialize 101010-2 format, error = EGL_SUCCESS
D/EGL_emulation( 5437): eglCreateContext: 0x72b870fa3b10: maj 2 min 0 rcv 2
D/EGL_emulation( 5437): eglMakeCurrent: 0x72b870fa3b10: ver 2 0 (tinfo 0x72ba8ae88180) (first time)
I/Gralloc4( 5437): mapper 4.x is not supported
I/TextToSpeech( 5437): Setting up the connection to TTS engine...
D/HostConnection( 5437): createUnique: call
D/HostConnection( 5437): HostConnection::get() New Host Connection established 0x72b870fa4f50, tid 5636
D/goldfish-address-space( 5437): allocate: Ask for block of size 0x100
D/goldfish-address-space( 5437): allocate: ioctl allocate returned offset 0x3edffc000 size 0x2000
W/Gralloc4( 5437): allocator 4.x is not supported
D/HostConnection( 5437): HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_YUV_Cache ANDROID_EMU_has_shared_slots_host_memory_allocator ANDROID_EMU_sync_buffer_data ANDROID_EMU_read_color_buffer_dma GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_2
D/HostConnection( 5437): createUnique: call
D/HostConnection( 5437): HostConnection::get() New Host Connection established 0x72b870fa3450, tid 5636
D/HostConnection( 5437): HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_YUV_Cache ANDROID_EMU_has_shared_slots_host_memory_allocator ANDROID_EMU_sync_buffer_data ANDROID_EMU_read_color_buffer_dma GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_2
D/EGL_emulation( 5437): eglMakeCurrent: 0x72b870fa2850: ver 2 0 (tinfo 0x72ba8ae88200) (first time)
D/EGL_emulation( 5437): eglCreateContext: 0x72b870fa3390: maj 2 min 0 rcv 2
D/FlutterGeolocator( 5437): Creating service.
D/FlutterGeolocator( 5437): Binding to location service.
D/FlutterGeolocator( 5437): Geolocator foreground service connected
D/FlutterGeolocator( 5437): Initializing Geolocator services
D/FlutterGeolocator( 5437): Flutter engine connected. Connected engine count 1
Connecting to VM Service at ws://127.0.0.1:52520/7Uq3vzWR_PI=/ws
W/FlutterJNI( 5437): FlutterJNI.loadLibrary called more than once
I/ResourceExtractor( 5437): Found extracted resources res_timestamp-133-1689879497352
W/FlutterJNI( 5437): FlutterJNI.prefetchDefaultFontManager called more than once
W/FlutterJNI( 5437): FlutterJNI.init called more than once
I/FLTFireBGExecutor( 5437): Creating background FlutterEngine instance, with args: [--start-paused, --enable-dart-profiling]
D/EGL_emulation( 5437): eglCreateContext: 0x72b870fa3d50: maj 2 min 0 rcv 2
D/EGL_emulation( 5437): eglCreateContext: 0x72b870fa4890: maj 2 min 0 rcv 2
D/HostConnection( 5437): createUnique: call
D/HostConnection( 5437): HostConnection::get() New Host Connection established 0x72b870f9ff10, tid 5741
D/HostConnection( 5437): HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_YUV_Cache ANDROID_EMU_has_shared_slots_host_memory_allocator ANDROID_EMU_sync_buffer_data ANDROID_EMU_read_color_buffer_dma GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_2
D/EGL_emulation( 5437): eglMakeCurrent: 0x72b870fa4890: ver 2 0 (tinfo 0x72ba8ae88280) (first time)
I/TextToSpeech( 5437): Sucessfully bound to com.google.android.tts
I/TextToSpeech( 5437): Connected to TTS engine
I/TextToSpeech( 5437): Setting up the connection to TTS engine...
W/SQLiteLog( 5437): (28) double-quoted string literal: "notification"
D/FlutterGeolocator( 5437): Geolocator foreground service connected
D/FlutterGeolocator( 5437): Initializing Geolocator services
D/FlutterGeolocator( 5437): Flutter engine connected. Connected engine count 2
D/EGL_emulation( 5437): app_time_stats: avg=478.81ms min=71.38ms max=1673.68ms count=4
I/flutter ( 5437): onPageChanged 1
I/flutter ( 5437): itemBuilder 1
D/EGL_emulation( 5437): app_time_stats: avg=265.48ms min=23.54ms max=1355.86ms count=7
D/EGL_emulation( 5437): app_time_stats: avg=146.04ms min=31.44ms max=1115.07ms count=10
I/flutter ( 5437): itemBuilder 2
I/flutter ( 5437): onPageChanged 2
I/flutter ( 5437): itemBuilder 3
I/flutter ( 5437): onPageChanged 3
D/EGL_emulation( 5437): app_time_stats: avg=55.15ms min=18.90ms max=258.53ms count=18
D/EGL_emulation( 5437): app_time_stats: avg=91.16ms min=22.30ms max=710.96ms count=12
I/flutter ( 5437): itemBuilder 4
I/flutter ( 5437): onPageChanged 4
D/EGL_emulation( 5437): app_time_stats: avg=50.04ms min=16.93ms max=503.90ms count=28
I/flutter ( 5437): itemBuilder 5
I/flutter ( 5437): onPageChanged 5
D/EGL_emulation( 5437): app_time_stats: avg=50.49ms min=19.49ms max=246.47ms count=21
I/flutter ( 5437): itemBuilder 6
I/flutter ( 5437): onPageChanged 6
D/EGL_emulation( 5437): app_time_stats: avg=50.42ms min=15.42ms max=441.49ms count=20
I/flutter ( 5437): itemBuilder 7
I/flutter ( 5437): onPageChanged 7
I/flutter ( 5437): itemBuilder 8
I/flutter ( 5437): onPageChanged 8
D/EGL_emulation( 5437): app_time_stats: avg=82.53ms min=18.27ms max=360.83ms count=14
I/flutter ( 5437): itemBuilder 9
I/flutter ( 5437): onPageChanged 9
D/EGL_emulation( 5437): app_time_stats: avg=31.93ms min=16.46ms max=59.25ms count=31
I/flutter ( 5437): itemBuilder 10
I/flutter ( 5437): onPageChanged 10
D/EGL_emulation( 5437): app_time_stats: avg=37.92ms min=15.08ms max=177.90ms count=27
I/flutter ( 5437): itemBuilder 11
I/flutter ( 5437): onPageChanged 11
D/EGL_emulation( 5437): app_time_stats: avg=49.81ms min=17.58ms max=456.65ms count=20
I/flutter ( 5437): itemBuilder 12
I/flutter ( 5437): onPageChanged 12
I/flutter ( 5437): itemBuilder 13
I/flutter ( 5437): onPageChanged 13
D/EGL_emulation( 5437): app_time_stats: avg=36.46ms min=15.46ms max=146.37ms count=27
D/EGL_emulation( 5437): app_time_stats: avg=49.67ms min=15.77ms max=438.82ms count=20
D/EGL_emulation( 5437): app_time_stats: avg=105.22ms min=19.10ms max=1083.31ms count=14
I/flutter ( 5437): itemBuilder 14
I/flutter ( 5437): onPageChanged 14
D/EGL_emulation( 5437): app_time_stats: avg=28.37ms min=15.43ms max=62.76ms count=34
D/EGL_emulation( 5437): app_time_stats: avg=26.62ms min=14.73ms max=38.46ms count=36
D/EGL_emulation( 5437): app_time_stats: avg=26.15ms min=14.88ms max=38.08ms count=36
D/EGL_emulation( 5437): app_time_stats: avg=26.83ms min=15.34ms max=67.52ms count=34
D/EGL_emulation( 5437): app_time_stats: avg=26.21ms min=14.47ms max=38.58ms count=37
D/EGL_emulation( 5437): app_time_stats: avg=53.53ms min=17.50ms max=354.10ms count=19
D/EGL_emulation( 5437): app_time_stats: avg=39.92ms min=24.09ms max=139.54ms count=25
D/EGL_emulation( 5437): app_time_stats: avg=34.88ms min=20.18ms max=75.62ms count=29
D/EGL_emulation( 5437): app_time_stats: avg=33.20ms min=20.06ms max=40.76ms count=30
D/EGL_emulation( 5437): app_time_stats: avg=34.26ms min=24.73ms max=51.62ms count=30
I/flutter ( 5437): itemBuilder 15
I/flutter ( 5437): onPageChanged 15
I/flutter ( 5437): itemBuilder 16
D/EGL_emulation( 5437): app_time_stats: avg=200.45ms min=22.77ms max=2003.17ms count=12
I/flutter ( 5437): onPageChanged 16
I/flutter ( 5437): itemBuilder 0