Skip to content

TabBar crash with SliverAppBar #158158

@hgraceb

Description

@hgraceb

Steps to reproduce

  1. Run example codes.
  2. Tap the top button to add a tab.

Expected results

No crash.

Actual results

Crashed.

Code sample

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

void main() {
  final List<Tab> tabs = <Tab>[];
  runApp(MaterialApp(
    home: StatefulBuilder(
      builder: (BuildContext context, StateSetter setState) {
        return DefaultTabController(
          length: tabs.length,
          child: Scaffold(
            body: CustomScrollView(
              slivers: <Widget>[
                SliverAppBar(
                  actions: <Widget>[
                    TextButton(
                      child: const Text('Add Tab'),
                      onPressed: () {
                        setState(() {
                          tabs.add(Tab(text: 'Tab ${tabs.length + 1}'));
                        });
                      },
                    ),
                  ],
                  bottom: TabBar(tabs: tabs),
                ),
              ],
            ),
          ),
        );
      },
    ),
  ));
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
======== Exception caught by widgets library =======================================================
The following RangeError was thrown building TabBar(dirty, dependencies: [InheritedCupertinoTheme, MediaQuery, _InheritedTheme, _LocalizationsScope-[GlobalKey#ea041], _TabControllerScope], state: _TabBarState#f6f77):
RangeError (index): Invalid value: Valid value range is empty: 0

The relevant error-causing widget was: 
  Column Column:file:///D:/Flutter/packages/flutter/lib/src/material/app_bar.dart:1101:16
When the exception was thrown, this was the stack: 
#0      List._setIndexed (dart:core-patch/growable_array.dart:274:49)
#1      List.[]= (dart:core-patch/growable_array.dart:269:5)
#2      _TabBarState.build.<anonymous closure> (package:flutter/src/material/tabs.dart:1752:21)
#3      new _GrowableList.generate (dart:core-patch/growable_array.dart:136:28)
#4      _TabBarState.build (package:flutter/src/material/tabs.dart:1741:38)
#5      StatefulElement.build (package:flutter/src/widgets/framework.dart:5748:27)
#6      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5636:15)
#7      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5799:11)
#8      Element.rebuild (package:flutter/src/widgets/framework.dart:5347:7)
#9      BuildScope._tryRebuild (package:flutter/src/widgets/framework.dart:2694:15)
#10     BuildScope._flushDirtyElements (package:flutter/src/widgets/framework.dart:2753:11)
#11     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:3048:18)
#12     WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:1176:21)
#13     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:475:5)
#14     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1397:15)
#15     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1318:9)
#16     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1176:5)
#17     _invoke (dart:ui/hooks.dart:312:13)
#18     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:427:5)
#19     _drawFrame (dart:ui/hooks.dart:283:31)
====================================================================================================

Flutter Doctor output

Doctor output
Flutter 3.27.0-1.0.pre.348 • channel [user-branch] • unknown source
Framework • revision 8ce47b6cb1 (20 hours ago) • 2024-11-04 20:26:49 +0800
Engine • revision 05cb5d7f79
Tools • Dart 3.7.0 (build 3.7.0-95.0.dev) • DevTools 2.40.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listc: crashStack traces logged to the consolef: material designflutter/packages/flutter/material repository.found in release: 3.24Found to occur in 3.24found in release: 3.27Found to occur in 3.27frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer versionteam-designOwned by Design Languages teamtriaged-designTriaged by Design Languages teamwaiting for PR to land (fixed)A fix is in flight

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions