New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Failed assertion: line 2283 pos 14: '_debugSubtreeRelayoutRootAlreadyMarkedNeedsLayout()': is not true #147452
Comments
@premiumbiscuit You are using |
Thanks @darshankawar for looking into this. I have replaced Code sampleimport 'dart:async';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
final GlobalKey<NavigatorState> _rootNavigatorKey =
GlobalKey<NavigatorState>(debugLabel: 'root');
final GlobalKey<NavigatorState> _sectionANavigatorKey =
GlobalKey<NavigatorState>(debugLabel: 'sectionANav');
class CounterStream {
final _stateController = StreamController<int>.broadcast();
int _counter = 0;
Stream<int> get stateStream => _stateController.stream.asBroadcastStream();
void increment() {
_stateController.sink.add(++_counter);
}
void dispose() {
_stateController.close();
}
}
final CounterStream counterStream = CounterStream();
void main() {
runApp(NestedTabNavigationExampleApp());
}
/// An example demonstrating how to use nested navigators
class NestedTabNavigationExampleApp extends StatelessWidget {
/// Creates a NestedTabNavigationExampleApp
NestedTabNavigationExampleApp({super.key});
final GoRouter _router = GoRouter(
navigatorKey: _rootNavigatorKey,
initialLocation: '/a/details',
routes: <RouteBase>[
StatefulShellRoute.indexedStack(
builder: (BuildContext context, GoRouterState state,
StatefulNavigationShell navigationShell) {
// Return the widget that implements the custom shell (in this case
// using a BottomNavigationBar). The StatefulNavigationShell is passed
// to be able access the state of the shell and to navigate to other
// branches in a stateful way.
return ScaffoldWithNavBar(navigationShell: navigationShell);
},
branches: <StatefulShellBranch>[
// The route branch for the first tab of the bottom navigation bar.
StatefulShellBranch(
navigatorKey: _sectionANavigatorKey,
routes: <RouteBase>[
GoRoute(
// The screen to display as the root in the first tab of the
// bottom navigation bar.
path: '/a',
builder: (BuildContext context, GoRouterState state) =>
const RootScreen(label: 'A', detailsPath: '/a/details'),
routes: <RouteBase>[
// The details screen to display stacked on navigator of the
// first tab. This will cover screen A but not the application
// shell (bottom navigation bar).
GoRoute(
path: 'details',
builder: (BuildContext context, GoRouterState state) =>
const DetailsScreen(label: 'A'),
),
],
),
],
),
],
),
],
);
@override
Widget build(BuildContext context) {
return MaterialApp.router(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
routerConfig: _router,
);
}
}
/// Builds the "shell" for the app by building a Scaffold with a
/// BottomNavigationBar, where [child] is placed in the body of the Scaffold.
class ScaffoldWithNavBar extends StatefulWidget {
/// Constructs an [ScaffoldWithNavBar].
const ScaffoldWithNavBar({
required this.navigationShell,
Key? key,
}) : super(key: key ?? const ValueKey<String>('ScaffoldWithNavBar'));
/// The navigation shell and container for the branch Navigators.
final StatefulNavigationShell navigationShell;
@override
State<ScaffoldWithNavBar> createState() => _ScaffoldWithNavBarState();
}
class _ScaffoldWithNavBarState extends State<ScaffoldWithNavBar> {
late StreamSubscription<int> _stateSubscription;
int _currentState = 0;
@override
void initState() {
super.initState();
_stateSubscription = counterStream.stateStream.listen((state) {
setState(() {
_currentState = state;
});
});
}
@override
void dispose() {
_stateSubscription.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraint) {
double width = constraint.maxWidth;
final Widget narrowContent = ConstrainedBox(
constraints: BoxConstraints(
minHeight: MediaQuery.of(context).size.height,
),
child: widget.navigationShell,
);
late final Widget body;
if (_currentState.isEven) {
body = Stack(children: [
Positioned.fill(child: narrowContent),
]);
} else {
body = Column(children: [Expanded(child: narrowContent)]);
}
final Widget rightArea = SafeArea(
// key: const Key('right_area'),
child: Align(
alignment: Alignment.center,
child: ConstrainedBox(
constraints: const BoxConstraints(
maxWidth: 800,
),
child: body,
),
),
);
return Scaffold(
body: rightArea,
bottomNavigationBar: BottomNavigationBar(
// Here, the items of BottomNavigationBar are hard coded. In a real
// world scenario, the items would most likely be generated from the
// branches of the shell route, which can be fetched using
// `navigationShell.route.branches`.
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home), label: 'Section A'),
BottomNavigationBarItem(
icon: Icon(Icons.work), label: 'Section B'),
BottomNavigationBarItem(
icon: Icon(Icons.tab), label: 'Section C'),
],
currentIndex: widget.navigationShell.currentIndex,
onTap: (int index) => _onTap(context, index),
),
);
},
);
}
/// Navigate to the current location of the branch at the provided index when
/// tapping an item in the BottomNavigationBar.
void _onTap(BuildContext context, int index) {
// When navigating to a new branch, it's recommended to use the goBranch
// method, as doing so makes sure the last navigation state of the
// Navigator for the branch is restored.
widget.navigationShell.goBranch(
index,
// A common pattern when using bottom navigation bars is to support
// navigating to the initial location when tapping the item that is
// already active. This example demonstrates how to support this behavior,
// using the initialLocation parameter of goBranch.
initialLocation: index == widget.navigationShell.currentIndex,
);
}
}
/// Widget for the root/initial pages in the bottom navigation bar.
class RootScreen extends StatefulWidget {
/// Creates a RootScreen
const RootScreen({
required this.label,
required this.detailsPath,
this.secondDetailsPath,
super.key,
});
/// The label
final String label;
/// The path to the detail page
final String detailsPath;
/// The path to another detail page
final String? secondDetailsPath;
@override
State<RootScreen> createState() => _RootScreenState();
}
class _RootScreenState extends State<RootScreen> {
late StreamSubscription<int> _stateSubscription;
int _currentState = 0;
@override
void initState() {
super.initState();
_stateSubscription = counterStream.stateStream.listen((state) {
setState(() {
_currentState = state;
});
});
}
@override
void dispose() {
_stateSubscription.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
if (_currentState.isEven) {
return Scaffold(body: const CircularProgressIndicator());
} else {
return Text('Screen ${widget.label}',
style: Theme.of(context).textTheme.titleLarge);
}
}
}
/// The details screen for either the A or B screen.
class DetailsScreen extends StatefulWidget {
/// Constructs a [DetailsScreen].
const DetailsScreen({
required this.label,
this.param,
this.extra,
this.withScaffold = true,
super.key,
});
/// The label to display in the center of the screen.
final String label;
/// Optional param
final String? param;
/// Optional extra object
final Object? extra;
/// Wrap in scaffold
final bool withScaffold;
@override
State<StatefulWidget> createState() => DetailsScreenState();
}
/// The state for DetailsScreen
class DetailsScreenState extends State<DetailsScreen> {
late StreamSubscription<int> _stateSubscription;
int _currentCount = 0;
@override
void initState() {
super.initState();
_stateSubscription = counterStream.stateStream.listen((state) {
setState(() {
_currentCount = state;
});
});
}
@override
void dispose() {
_stateSubscription.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
if (widget.withScaffold) {
return Scaffold(
appBar: AppBar(
title: Text('Details Screen - ${widget.label}'),
),
body: _build(context),
);
} else {
return ColoredBox(
color: Theme.of(context).scaffoldBackgroundColor,
child: _build(context),
);
}
}
Widget _build(BuildContext context) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text('Details for ${widget.label} - Counter: ${_currentCount}',
style: Theme.of(context).textTheme.titleLarge),
const Padding(padding: EdgeInsets.all(4)),
TextButton(
onPressed: () {
setState(() {
counterStream.increment();
});
},
child: const Text('Crash me!'),
),
const Padding(padding: EdgeInsets.all(8)),
if (widget.param != null)
Text('Parameter: ${widget.param!}',
style: Theme.of(context).textTheme.titleMedium),
const Padding(padding: EdgeInsets.all(8)),
if (widget.extra != null)
Text('Extra: ${widget.extra!}',
style: Theme.of(context).textTheme.titleMedium),
if (!widget.withScaffold) ...<Widget>[
const Padding(padding: EdgeInsets.all(16)),
TextButton(
onPressed: () {
GoRouter.of(context).pop();
},
child: const Text('< Back',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18)),
),
]
],
),
);
}
}
|
Thanks for the update. Using above code sample, it throws below error log in the console: error log
stable, master flutter doctor -v
|
Thank you for the report. To debug this it would help if you could reduce the test case more, see https://github.com/flutter/flutter/wiki/Issue-hygiene#provide-reduced-test-cases |
I tried to come up with reduced repro code as below without go_router implementation but it is giving me different error, so probably @premiumbiscuit please try to narrow down the code to bare minimum that still triggers the reported error. |
Steps to reproduce
I have attached a sample code to reproduce the bug. Click the "crash me!" button twice. This issue seems to disappear when the layout builder is removed.
Expected results
The counter should increment without any crashes.
Actual results
A crash occurs.
Code sample
Code sample
Pubspec
Screenshots or Video
Screenshots / Video demonstration
[Upload media here]
Logs
Logs
Flutter Doctor output
Doctor output
The text was updated successfully, but these errors were encountered: