Skip to content

TabBar throws an error on back navigation when used together with initialRoute #152837

@vendik

Description

@vendik

Steps to reproduce

  1. Run the example code
  2. Press the button to navigate back
  3. Observe logs in the console

Expected results

No error

Actual results

Error, see the Logs section

Code sample

Code sample
import 'package:flutter/material.dart';

void main() => runApp(App());

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      initialRoute: '/deep',
      routes: {
        '/': (context) => RootScreen(),
        '/deep': (context) => DeepScreen(),
      },
    );
  }
}

class RootScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return const DefaultTabController(
      length: 0,
      child: Scaffold(
        backgroundColor: Colors.white,
        body: TabBarView(children: []),
        bottomNavigationBar: TabBar(tabs: []),
      ),
    );
  }
}

class DeepScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      child: Text('Click to go back to RootScreen'),
      onPressed: () => Navigator.pop(context),
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
======== Exception caught by widgets library =======================================================
The following _TypeError was thrown while rebuilding dirty elements:
Null check operator used on a null value

The relevant error-causing widget was: 
  TabBarView TabBarView:file:///C:/Users/Admin/Projects/tab_crash/lib/main.dart:25:15
When the exception was thrown, this was the stack: 
#0      ScrollPosition.viewportDimension (package:flutter/src/widgets/scroll_position.dart:260:53)
#1      _PagePosition.getPixelsFromPage (package:flutter/src/widgets/page_view.dart:381:19)
#2      PageController.jumpToPage (package:flutter/src/widgets/page_view.dart:215:30)
#3      _TabBarViewState.didChangeDependencies (package:flutter/src/material/tabs.dart:1976:24)
#4      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5647:13)
#5      Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#6      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2905:19)
#7      WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:1136:21)
#8      RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:443:5)
#9      SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1392:15)
#10     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1313:9)
#11     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1171:5)
#12     _invoke (dart:ui/hooks.dart:312:13)
#13     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:419:5)
#14     _drawFrame (dart:ui/hooks.dart:283:31)
The element being rebuilt at the time was index 14 of 15: TabBarView
  dirty
  dependencies: [_TabControllerScope]
  state: _TabBarViewState#2d5ab
====================================================================================================

======== Exception caught by scheduler library =====================================================
The following assertion was thrown during a scheduler callback:
buildScope missed some dirty elements.


This probably indicates that the dirty list should have been resorted but was not.

The list of dirty elements at the end of the buildScope call was: 
  : ElevatedButton
    dependencies: [MediaQuery, _InheritedTheme, _LocalizationsScope-[GlobalKey#6a39c]]
    state: _ButtonStyleState#8fa4d
  : DefaultTabController
    dependencies: [_EffectiveTickerMode]
    state: _DefaultTabControllerState#7acad(ticker inactive)
  : TabBar
    dependencies: [_InheritedTheme, _LocalizationsScope-[GlobalKey#6a39c], _TabControllerScope]
    state: _TabBarState#d9092
  : TabBarView
    dirty
    dependencies: [_TabControllerScope]
    state: _TabBarViewState#2d5ab
  ...
When the exception was thrown, this was the stack: 
#0      BuildOwner.buildScope.<anonymous closure> (package:flutter/src/widgets/framework.dart:2943:11)
#1      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2950:8)
#2      WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:1136:21)
#3      RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:443:5)
#4      SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1392:15)
#5      SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1313:9)
#6      SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1171:5)
#7      _invoke (dart:ui/hooks.dart:312:13)
#8      PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:419:5)
#9      _drawFrame (dart:ui/hooks.dart:283:31)
====================================================================================================

Flutter Doctor output

Doctor output
[√] Flutter (Channel stable, 3.22.3, on Microsoft Windows [Version 10.0.19044.1288], locale sv-SE)
    • Flutter version 3.22.3 on channel stable at C:\Users\Admin\AppData\Local\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision b0850beeb2 (3 weeks ago), 2024-07-16 21:43:41 -0700
    • Engine revision 235db911ba
    • Dart version 3.4.4
    • DevTools version 2.34.3

[√] Windows Version (Installed version of Windows is version 10 or higher)

[!] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at C:\Users\Admin\AppData\Local\Android\sdk
    X cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    X Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.

[√] Chrome - develop for the web
    • Chrome at C:\Users\Admin\AppData\Local\Google\Chrome\Application\chrome.exe

[X] Visual Studio - develop Windows apps
    X Visual Studio not installed; this is necessary to develop Windows apps.
      Download at https://visualstudio.microsoft.com/downloads/.
      Please install the "Desktop development with C++" workload, including all of its default components

    • Windows (desktop) • windows     • windows-x64    • Microsoft Windows [Version 10.0.19044.1288]
    • Chrome (web)      • chrome      • web-javascript • Google Chrome 127.0.6533.89
    • Edge (web)        • edge        • web-javascript • Microsoft Edge 126.0.2592.102

[√] Network resources
    • All expected network resources are available.

! Doctor found issues in 2 categories.

Metadata

Metadata

Assignees

Labels

a: error messageError messages from the Flutter frameworkf: material designflutter/packages/flutter/material repository.f: routesNavigator, Router, and related APIs.found in release: 3.22Found to occur in 3.22found in release: 3.24Found to occur in 3.24frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: duplicateIssue is closed as a duplicate of an existing issueteam-designOwned by Design Languages team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions