Repository navigation
Can't access PageController.page even when hasClients is true #146986
Copy link
Copy link
Closed
Labels
P2Important issues not at the top of the work listImportant issues not at the top of the work listf: scrollingViewports, list views, slivers, etc.Viewports, list views, slivers, etc.found in release: 3.19Found to occur in 3.19Found to occur in 3.19found in release: 3.22Found to occur in 3.22Found to occur in 3.22frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer versionIssue is closed as already fixed in a newer versionteam-frameworkOwned by Framework teamOwned by Framework teamtriaged-frameworkTriaged by Framework teamTriaged by Framework team
Description
Steps to reproduce
- Create a page view with a page controller
- Put a builder or subwidget inside of
pages - Display
PageController.pagefrom inside the builder/subwidget, checkingcontroller.hasClientsfirst
Expected results
The current page is displayed or a fallback if no page is available (has client is false or the page is null)
Actual results
Page value is only available after content dimensions are established. is thrown and the screen flashes red before properly displaying the page number.
Code sample
Code sample
class PageDemo extends StatefulWidget {
const PageDemo({super.key});
@override
State<PageDemo> createState() => _PageDemoState();
}
class _PageDemoState extends State<PageDemo> {
final PageController controller = PageController();
@override
Widget build(BuildContext context) {
if (controller.hasClients) {
print(controller.page);
}
return MaterialApp(
home: Scaffold(
body: PageView(
controller: controller,
children: [
// Only throws if a builder or sub widget is used here.
Builder(
builder: (context) {
final double currentPage =
controller.hasClients ? controller.page ?? -1 : -2;
return Center(child: Text(currentPage.toString()));
},
),
],
),
),
);
}
}Screenshots or Video
Screenshots / Video demonstration
[Upload media here]
Logs
Logs
[Paste your logs here]Flutter Doctor output
Doctor output
[Paste your output here]Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
P2Important issues not at the top of the work listImportant issues not at the top of the work listf: scrollingViewports, list views, slivers, etc.Viewports, list views, slivers, etc.found in release: 3.19Found to occur in 3.19Found to occur in 3.19found in release: 3.22Found to occur in 3.22Found to occur in 3.22frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer versionIssue is closed as already fixed in a newer versionteam-frameworkOwned by Framework teamOwned by Framework teamtriaged-frameworkTriaged by Framework teamTriaged by Framework team
Activity
caseycrogers commentedon Apr 18, 2024
Relatedly, why is
PageController.pagenullable when it throws if the page isn't available? It feels like an API should either return null if a valid value isn't available or throw-not both?danagbemava-nc commentedon Apr 18, 2024
Reproducible using the code sample provided above.
complete sample
flutter doctor -v
content dimensions are not established get controller value error (#1…
content dimensions are not established get controller value error (fl…
Piinks commentedon Jul 25, 2024
Fixed by #148938
github-actions commentedon Aug 9, 2024
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.