実現したいこと
エラーの出ないようにTabbarを用いた画面遷移を行いたいです。
発生している問題・分からないこと
TabbarをFlutterアプリケーションに実装しました。ビルドは成功するのですが、Tabbarのボタンを押して画面遷移するタイミングで、デバッグログに、大量のエラーメッセージが出力されました。
エラーメッセージ
error
1======== Exception caught by animation library ===================================================== 2The following assertion was thrown while notifying listeners for AnimationController: 3setState() called after dispose(): _AnimatedState#16d19(lifecycle state: defunct, not mounted) 4 5This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback. 6 7The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree. 8This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose(). 9 10When the exception was thrown, this was the stack: 11#0 State.setState.<anonymous closure> (package:flutter/src/widgets/framework.dart:1167:9) 12#1 State.setState (package:flutter/src/widgets/framework.dart:1202:6) 13#2 _AnimatedState._handleChange (package:flutter/src/widgets/transitions.dart:129:5) 14#3 AnimationLocalListenersMixin.notifyListeners (package:flutter/src/animation/listener_helpers.dart:161:19) 15#4 AnimationController._tick (package:flutter/src/animation/animation_controller.dart:865:5) 16#5 Ticker._tick (package:flutter/src/scheduler/ticker.dart:258:12) 17#6 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1386:15) 18#7 SchedulerBinding.handleBeginFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:1233:11) 19#8 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:633:13) 20#9 SchedulerBinding.handleBeginFrame (package:flutter/src/scheduler/binding.dart:1231:17) 21#10 SchedulerBinding._handleBeginFrame (package:flutter/src/scheduler/binding.dart:1148:5) 22#11 _invoke1 (dart:ui/hooks.dart:328:13) 23#12 PlatformDispatcher._beginFrame (dart:ui/platform_dispatcher.dart:361:5) 24#13 _beginFrame (dart:ui/hooks.dart:272:31) 25The AnimationController notifying listeners was: AnimationController#7f69a(▶ 1.000) 26==================================================================================================== 27 28(以下、大量に同様のメッセージ)
該当のソースコード
Dart
1import 'package:flutter/material.dart'; 2 3void main() { 4 runApp(const MyApp()); 5} 6 7class MyApp extends StatelessWidget { 8 const MyApp({super.key}); 9 10 11 Widget build(BuildContext context) { 12 return MaterialApp( 13 title: 'Test app', 14 theme: ThemeData( 15 colorScheme: ColorScheme.fromSeed(seedColor: Colors.green), 16 useMaterial3: true, 17 ), 18 home: const MyHomePage(title: 'テストアプリ'), 19 ); 20 } 21} 22 23class MyHomePage extends StatefulWidget { 24 const MyHomePage({super.key, required this.title}); 25 26 final String title; 27 28 29 State<MyHomePage> createState() => _MyHomePageState(); 30} 31 32class _MyHomePageState extends State<MyHomePage> { 33 34 Widget build(BuildContext context) { 35 return DefaultTabController( 36 length: 2, 37 child:Scaffold( 38 appBar: AppBar( 39 backgroundColor: Theme.of(context).colorScheme.inversePrimary, 40 title: Text(widget.title), 41 42 // タブバー 43 bottom: const TabBar( 44 tabs: <Widget>[ 45 Tab(icon: Text("1")), 46 Tab(icon:Text("2")), 47 ] 48 ), 49 ), 50 51 // タブ別コンテンツ 52 body: const TabBarView(children: <Widget>[ 53 Center(child: Text("page 1")), 54 Center(child: Text("page 2")), 55 ]) 56 ), 57 ); 58 } 59} 60
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
"SetState()が関係している"という原因の内容しかありませんでした。少なくともこのソースコード内ではSetStateは実装していないので、探した限りでは、同じケースに遭遇する人は見つかりませんでした。
補足
ビルドはmacOS Desktopで行っています。
Chromeでのビルドでは、ログは発生しませんでした。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。