Repository: flutter
Issue: Removed Tab from TabController is still scrollable by swiping in the TabBarView
Labels: framework,f: material design,f: scrolling,a: quality,has reproducible steps,P2,found in release: 3.3,found in release: 3.7,team-design,triaged-design

Hey guys,

I've been writing a tutorial about Dynamic TabBar and TabBarView and I discovered something interesting. It seems somehow Widgets are cached while they are dynamically removed.

Repo - https://github.com/zeromaro/dynamic-tab-bar-view/tree/tbv-basic .

1. Run the app.
2. Try to click on different TabViews - everything works as expected. Tabs and TabViews are switching.
3. Now click on TabView with number **3** and press **-** (minus sign will remove TabView and Removed Widget too). However, if we now **horizontally scroll to left**, we can access last widget, which is removed.

Log below
```
E/flutter (15672): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: 'package:flutter/src/material/tab_controller.dart': Failed assertion: line 152 pos 12: 'value >= 0 && (value < length || length == 0)': is not true.
E/flutter (15672): #0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:40:39)
E/flutter (15672): #1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:36:5)
E/flutter (15672): #2      TabController._changeIndex (package:flutter/src/material/tab_controller.dart:152:12)
E/flutter (15672): #3      TabController.index= (package:flutter/src/material/tab_controller.dart:188:5)
E/flutter (15672): #4      _TabBarViewState._handleScrollNotification (package:flutter/src/material/tabs.dart:1307:19)
E/flutter (15672): #5      NotificationListener._dispatch (package:flutter/src/widgets/notification_listener.dart:127:27)
E/flutter (15672): #6      Notification.visitAncestor (package:flutter/src/widgets/notification_listener.dart:45:20)
E/flutter (15672): #7      ViewportNotificationMixin.visitAncestor (package:flutter/src/widgets/scroll_notification.dart:31:18)
E/flutter (15672): #8      Element.visitAncestorElements (package:flutter/src/widgets/framework.dart:3502:39)
E/flutter (15672): #9      Notification.dispatch (package:flutter/src/widgets/notification_listener.dart:61:13)
E/flutter (15672): #10     ScrollActivity.dispatchScrollEndNotification (package:flutter/src/widgets/scroll_activity.dart:106:63)
E/flutter (15672): #11     ScrollPosition.didEndScroll (package:flutter/src/widgets/scroll_position.dart:670:14)
E/flutter (15672): #12     ScrollPosition.beginActivity (package:flutter/src/widgets/scroll_position.dart:639:9)
E/flutter (15672): #13     ScrollPositionWithSingleContext.beginActivity (package:flutter/src/widgets/scroll_position_with_single_context.dart:116:11)
E/flutter (15672): #14     ScrollPositionWithSingleContext.goIdle (package:flutter/src/widgets/scroll_position_with_single_context.dart:131:5)
E/flutter (15672): #15     ScrollPositionWithSingleContext.goBallistic (package:flutter/src/widgets/scroll_position_with_single_context.dart:150:7)
E/flutter (15672): #16     BallisticScrollActivity._end (package:flutter/src/widgets/scroll_activity.dart:551:15)
E/flutter (15672): #17     _rootRun (dart:async/zone.dart:1120:38)
E/flutter (15672): #18     _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter (15672): #19     _FutureListener.handleWhenComplete (dart:async/future_impl.dart:161:18)
E/flutter (15672): #20     Future._propagateToListeners.handleWhenCompleteCallback (dart:async/future_impl.dart:648:39)
E/flutter (15672): #21     Future._propagateToListeners (dart:async/future_impl.dart:704:37)
E/flutter (15672): #22     Future._completeWithValue (dart:async/future_impl.dart:522:5)
E/flutter (15672): #23     Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:552:7)
E/flutter (15672): #24     _rootRun (dart:async/zone.dart:1124:13)
E/flutter (15672): #25     _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter (15672): #26     _CustomZone.runGuarded (dart:async/zone.dart:923:7)
E/flutter (15672): #27     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:963:23)
E/flutter (15672): #28     _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter (15672): #29     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
E/flutter (15672):
```
This is first part of discovery.
Second part is that now we have 3 tabs. Originally we had 4 tabs. Let's click **+** sign two times. Second time our UI will overflow.
```

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following RangeError was thrown building TabBar(dirty, dependencies: [_LocalizationsScope-[GlobalKey#36d65], _InheritedTheme], state: _TabBarState#bedd5):
RangeError (index): Invalid value: Not in range 0..3, inclusive: 4

User-created ancestor of the error-causing widget was: 
  AppBar file:///Users/mac/FlutterProject/tutorials/dynamic_tab_bar_view/lib/main.dart:46:15
When the exception was thrown, this was the stack: 
#0      List.[] (dart:core-patch/growable_array.dart:147:60)
#1      _TabBarState.build (package:flutter/src/material/tabs.dart:1017:26)
#2      StatefulElement.build (package:flutter/src/widgets/framework.dart:4040:27)
#3      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3934:15)
#4      Element.rebuild (package:flutter/src/widgets/framework.dart:3731:5)
...
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ (2) Exception caught by rendering library ═════════════════════════════════════════════════
A RenderFlex overflowed by 99896 pixels on the bottom.
User-created ancestor of the error-causing widget was: 
  AppBar file:///Users/mac/FlutterProject/tutorials/dynamic_tab_bar_view/lib/main.dart:46:15
════════════════════════════════════════════════════════════════════════════════════════════════════
```
I'm not sure it's expected or not.

Flutter Doctor
```
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel dev, v1.10.5, on Mac OS X 10.15 19A583, locale en-HK)
 
[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
[✓] Xcode - develop for iOS and macOS (Xcode 11.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 3.5)
[✓] VS Code (version 1.39.0)
[✓] Connected device (3 available)

• No issues found!
```


Cheers