Skip to content

_handleChange in _AnimationState throws (setState() called after dispose(): _AnimatedState#e2d24(lifecycle state: defunct, not mounted)) #144087

@RolfSuslik

Description

@RolfSuslik

Steps to reproduce

I updated flutter today and noticed the following error:

I use Riverpod to update my states. (Reproducible without Riverpod too. See first comment)
I handle the state of the content and number of tabs of my TabBar with it.
When I add a new tab and update the state with Riverpod, the TabBar gets rebuild.
When I try to open the new created tab, the app crashes. The error:
"setState() called after dispose(): _AnimatedState#e2d24(lifecycle state: defunct, not mounted)"
I looked into the stack trace and noticed that there is a method:

void _handleChange() {
    setState(() {
      // The listenable's state is our build state, and it changed already.
    });
  }

and its in C:\dev\flutter\packages\flutter\lib\src\widgets\transitions.dart_AnimatedState_handleChange

When I comment out the "setState" my app works again.

Expected results

When I create new tabs in my TabBar, I should be able to select them.

Actual results

The app crashes when I try to select the new tab with the error message: "setState() called after dispose(): _AnimatedState#e2d24(lifecycle state: defunct, not mounted)"

Code sample

Code sample
final tabsProvider = StateProvider<int>((ref) {
  return 1;
});

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  static const String _title = 'Flutter Stateful Clicker Counter';
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: _title,
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends ConsumerWidget {
  const MyHomePage({super.key});
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final tabs = ref.watch(tabsProvider);
    return DefaultTabController(
      length: tabs,
      child: Scaffold(
        appBar: AppBar(
          title: const Text('Flutter Demo Click Counter'),
          bottom: TabBar(
            tabAlignment: TabAlignment.start,
            isScrollable: true,
            tabs: createTabs(tabs),
          ),
        ),
        body: TabBarView(
          children: createTabViews(tabs),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () => ref.read(tabsProvider.notifier).state = tabs + 1,
          tooltip: 'Increment',
          child: const Icon(Icons.add),
        ),
      ),
    );
  }
}

List<Tab> createTabs(int tabs) {
  final List<Tab> tabList = [];
  for (var i = 0; i < tabs; i++) {
    tabList.add(Tab(
      text: "Tab $i",
    ));
  }
  return tabList;
}

List<Text> createTabViews(int tabs) {
  final List<Text> allTabs = [];
  for (var i = 0; i < tabs; i++) {
    allTabs.add(Text("View $i"));
  }
  return allTabs;
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[√] Flutter (Channel stable, 3.19.1, on Microsoft Windows [Version 10.0.22631.3155], locale en-CH)
    • Flutter version 3.19.1 on channel stable at C:\dev\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision abb292a07e (4 days ago), 2024-02-20 14:35:05 -0800
    • Engine revision 04817c99c9
    • Dart version 3.3.0
    • DevTools version 2.31.1

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

[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at C:\Users\rsu\AppData\Local\Android\sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-b2043.56-10550314)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.1.5)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.1.32414.318
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2023.1)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-b2043.56-10550314)

[√] IntelliJ IDEA Ultimate Edition (version 2022.2)
    • IntelliJ at C:\Users\rsu\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\222.4345.14
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart

[√] VS Code (version 1.86.2)
    • VS Code at C:\Users\rsu\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.82.0

[√] Connected device (4 available)
    • sdk gphone64 x86 64 (mobile) • emulator-5554 • android-x64    • Android 14 (API 34) (emulator)
    • Windows (desktop)            • windows       • windows-x64    • Microsoft Windows [Version 10.0.22631.3155]
    • Chrome (web)                 • chrome        • web-javascript • Google Chrome 121.0.6167.162
    • Edge (web)                   • edge          • web-javascript • Microsoft Edge 121.0.2277.128

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

• No issues found!

Metadata

Metadata

Assignees

Labels

P1High-priority issues at the top of the work lista: error messageError messages from the Flutter frameworkfound in release: 3.19Found to occur in 3.19found in release: 3.20Found to occur in 3.20frameworkflutter/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-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions