Repository navigation
Hot reload doesn't recognize change in length in DefaultTabController #18686
Copy link
Copy link
Closed
Labels
c: crashStack traces logged to the consoleStack traces logged to the consoleframeworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.p: material_uimaterial_ui package in flutter/packagesmaterial_ui package in flutter/packagest: hot reloadReloading code during "flutter run"Reloading code during "flutter run"
Description
When updating the DefaultTabController from the tutorial to a length of 4 instead of 3 and reloading, Flutter doesn't recognize that the length value has changed.
This is the code:
import 'package:flutter/material.dart';
void main() => runApp(new HeadacheApp());
class HeadacheApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 4,
child: Scaffold(
appBar: AppBar(
bottom: TabBar(
tabs: [
Tab(icon: Icon(Icons.directions_car)),
Tab(icon: Icon(Icons.directions_transit)),
Tab(icon: Icon(Icons.directions_bike)),
Tab(icon: Icon(Icons.help)),
],
),
title: Text('Tabs Demo'),
),
body: TabBarView(
children: [
Icon(Icons.directions_car),
Icon(Icons.directions_transit),
Icon(Icons.directions_bike),
Icon(Icons.help),
],
),
),
),
);
}
}It gives the following error:
flutter: ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
flutter: The following assertion was thrown while handling a gesture:
flutter: 'package:flutter/src/material/tab_controller.dart': Failed assertion: line 112 pos 12: 'value >= 0
flutter: && (value < length || length == 0)': is not true.
flutter:
flutter: Either the assertion indicates an error in the framework itself, or we should provide substantially
flutter: more information in this error message to help you determine and fix the underlying cause.
flutter: In either case, please report this assertion by filing a bug on GitHub:
flutter: https://github.com/flutter/flutter/issues/new
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #2 TabController._changeIndex (package:flutter/src/material/tab_controller.dart)
flutter: #3 TabController.animateTo (package:flutter/src/material/tab_controller.dart:164:5)
flutter: #4 _TabBarState._handleTap (package:flutter/src/material/tabs.dart:864:17)
flutter: #5 _TabBarState.build.<anonymous closure> (package:flutter/src/material/tabs.dart:939:21)
flutter: #6 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:494:14)
flutter: #7 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:549:30)
flutter: #8 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
flutter: #9 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:161:9)
flutter: #10 TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:94:7)
flutter: #11 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:315:9)
flutter: #12 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
flutter: #13 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
flutter: #14 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:143:19)
flutter: #15 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:121:22)
flutter: #16 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:101:7)
flutter: #17 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:64:7)
flutter: #18 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:48:7)
flutter: #19 _invoke1 (dart:ui/hooks.dart:134:13)
flutter: #20 _dispatchPointerDataPacket (dart:ui/hooks.dart:91:5)
flutter: (elided 2 frames from class _AssertionError)
flutter:
flutter: Handler: onTap
flutter: Recognizer:
flutter: TapGestureRecognizer#8e887(debugOwner: GestureDetector, state: possible, won arena, finalPosition:
flutter: Offset(329.3, 132.7), sent tap down)
flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════
I went into flutter/src/material/tab_controller.dart and printed the values of length and value which were both 3 instead of 3 and 4, which caused the exception to be raised. I was able to fix this by restarting the iPhone simulator and re-uploading the app.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
c: crashStack traces logged to the consoleStack traces logged to the consoleframeworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.p: material_uimaterial_ui package in flutter/packagesmaterial_ui package in flutter/packagest: hot reloadReloading code during "flutter run"Reloading code during "flutter run"
Activity
zoechi commentedon Jun 22, 2018
/cc @cbracken
zoechi commentedon Jun 22, 2018
@zachjanicki
Please add the output of
flutter doctorcbracken commentedon Jul 9, 2018
/cc @rmacnak-google who IIRC looked into similar issues in the past (specifically detecting and warning about changes that aren't reloaded).
no-or-no commentedon Jul 17, 2018
similar issue!
my code:
after 2s, tap item4 and item5 throw exception:
flutter doctor:
matthew5025 commentedon Aug 3, 2018
Is this related to #18481?
zoechi commentedon Aug 3, 2018
@matthew5025 are you sure this is the link you intended to post. That doesn't look related to me.
matthew5025 commentedon Aug 4, 2018
@zoechi Apologies, it should have been #18481
AbdulRahmanAlHamali commentedon Sep 6, 2018
Facing the same issue when adding a tab dynamically to the tab controller
AbdulRahmanAlHamali commentedon Sep 26, 2018
The problem seems to be with using
DefaultTabController, because it creates aTabControllerin itsinitState, and assigns it a length, which means the tab controller cannot change the length dynamically. To solve an issue similar to this, where I had to add extra tabs depending on certain conditions, I just used a customTabControllerinstead of theDefaultTabControllerflypigz commentedon Jan 19, 2019
This is not only a hot reload issue, the
DefaultTabController/TabControllerdoes not support dynamic changing the length.I meet the same issue, I need to dynamic changing the length of TabController when refreshing data from network. This is a product requirement.
@AbdulRahmanAlHamali How do you create a custom
TabController? remember we need to call thedispose()method of aTabController, otherwise it will lead to memory leak.I assign a
ValueKey(using the length of tabs as value ) to theDefaultTabController, and it works correctly, because when the key changes, the Flutter wont reuse the currentDefaultTabController.This is a temp fix, hope this issue to be fixed ASAP.
AbdulRahmanAlHamali commentedon Jan 19, 2019
@MaxZeng I just create a
TabControllerinside the widget and assign it to theTabBar, and every time I want to change the number of tabs in the tab controller, I dispose of the old one and create a new one with the new number of tabs.dbpprt commentedon Feb 5, 2019
Is there any progress or ETA on this issue?
w4mxl commentedon Mar 14, 2019
Is this issue still being worked on?
PandaGeek1024 commentedon Mar 20, 2019
the cause is exactly what @AbdulRahmanAlHamali said. Any update on this, I don't see the fix in v1.2.1 yet? @zoechi
github-actions commentedon Aug 30, 2021
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.