Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setState() called after dispose() #75

Closed
pradeep14598 opened this issue Mar 22, 2022 · 11 comments
Closed

setState() called after dispose() #75

pradeep14598 opened this issue Mar 22, 2022 · 11 comments

Comments

@pradeep14598
Copy link

E/flutter ( 7192): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: setState() called after dispose(): _GNavState#fb094(lifecycle state: defunct, not mounted)
E/flutter ( 7192): This 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.
E/flutter ( 7192): The 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.
E/flutter ( 7192): This 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().

@sooxt98
Copy link
Owner

sooxt98 commented Mar 22, 2022 via email

@pradeep14598
Copy link
Author

pradeep14598 commented Mar 22, 2022

could u provide a minimal reproducible code?

On Tue, 22 Mar 2022 at 9:46 PM, Pk @.> wrote: E/flutter ( 7192): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: setState() called after dispose(): _GNavState#fb094(lifecycle state: defunct, not mounted) E/flutter ( 7192): This 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. E/flutter ( 7192): The 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. E/flutter ( 7192): This 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(). — Reply to this email directly, view it on GitHub <#75>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADGCEC7AHFHPEMJLAFM7D73VBHFJ7ANCNFSM5RK6EZ7A . You are receiving this because you are subscribed to this thread.Message ID: @.>

 bottomNavigationBar: Container(
            //   height: kBottomNavigationBarHeight,
            decoration: BoxDecoration(
              color: Colors.white,
              boxShadow: [
                BoxShadow(
                  blurRadius: 20,
                  color: Colors.black.withOpacity(.1),
                )
              ],
            ),
            child: SafeArea(
              child: Padding(
                padding:
                    const EdgeInsets.symmetric(horizontal: 24.0, vertical: 8),
                child: GNav(
                  rippleColor: AppColor.surfaceDisabled1,
                  hoverColor: AppColor.surfaceDisabled1,
                  gap: 4,
                  activeColor: AppColor.splash,
                  iconSize: 24,
                  padding:
                      const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
                  tabBackgroundColor: AppColor.surfaceDisabled1,
                  color: AppColor.surfaceDisabled1,
                  tabs: [
                    GButton(
                      leading: Image.asset(
                        Images.tab1,
                        height: 24,
                        width: 24,
                      ),
                      text: 'tab1'.tr,
                      textStyle: AppTextStyle.addBtnTxt
                          .copyWith(color: AppColor.primaryFontBlack),
                      icon: null,
                    ),
                    GButton(
                      leading: Image.asset(
                        Images.tab2,
                        height: 24,
                        width: 24,
                      ),
                      text: 'tab2'.tr,
                      textStyle: AppTextStyle.addBtnTxt
                          .copyWith(color: AppColor.primaryFontBlack),
                      icon: null,
                    ),
                    GButton(
                      leading: Image.asset(
                        Images.tab3,
                        height: 24,
                        width: 24,
                      ),
                      text: 'tab3'.tr,
                      textStyle: AppTextStyle.addBtnTxt
                          .copyWith(color: AppColor.primaryFontBlack),
                      icon: null,
                    ),
                    GButton(
                      leading: Image.asset(
                        Images.tab4,
                        height: 24,
                        width: 24,
                      ),
                      text: 'tab4'.tr,
                      textStyle: AppTextStyle.addBtnTxt
                          .copyWith(color: AppColor.primaryFontBlack),
                      icon: null,
                    ),
                  ],
                  selectedIndex: _selectedIndex,
                  onTabChange: (index) {
                    if (mounted)
                      setState(() {
                        _selectedIndex = index;
                        _onItemTapped(index);
                      });
                  },
                ),
              ),
            ),
          ),

@sooxt98
Copy link
Owner

sooxt98 commented Mar 22, 2022 via email

@pradeep14598
Copy link
Author

pradeep14598 commented Mar 22, 2022

I’m not able to run this, onItemTapped is missing

On Tue, 22 Mar 2022 at 9:59 PM, Pk @.***> wrote: could u provide a minimal reproducible code? … <#m_587440306533223400
> On Tue, 22 Mar 2022 at 9:46 PM, Pk @.> wrote: E/flutter ( 7192): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: setState() called after dispose(): _GNavState#fb094(lifecycle state: defunct, not mounted) E/flutter ( 7192): This 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. E/flutter ( 7192): The 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. E/flutter ( 7192): This 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(). — Reply to this email directly, view it on GitHub <#75 <#75>>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADGCEC7AHFHPEMJLAFM7D73VBHFJ7ANCNFSM5RK6EZ7A https://github.com/notifications/unsubscribe-auth/ADGCEC7AHFHPEMJLAFM7D73VBHFJ7ANCNFSM5RK6EZ7A . You are receiving this because you are subscribed to this thread.Message ID: @.> bottomNavigationBar: Container( // height: kBottomNavigationBarHeight, decoration: BoxDecoration( color: Colors.white, boxShadow: [ BoxShadow( blurRadius: 20, color: Colors.black.withOpacity(.1), ) ], ), child: SafeArea( child: Padding( padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 8), child: GNav( rippleColor: AppColor.surfaceDisabled1, hoverColor: AppColor.surfaceDisabled1, gap: 4, activeColor: AppColor.splash, iconSize: 24, padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6), tabBackgroundColor: AppColor.surfaceDisabled1, color: AppColor.surfaceDisabled1, tabs: [ GButton( leading: Image.asset( Images.tab1, height: 24, width: 24, ), text: 'tab1'.tr, textStyle: AppTextStyle.addBtnTxt .copyWith(color: AppColor.primaryFontBlack), icon: null, ), GButton( leading: Image.asset( Images.tab2, height: 24, width: 24, ), text: 'tab2'.tr, textStyle: AppTextStyle.addBtnTxt .copyWith(color: AppColor.primaryFontBlack), icon: null, ), GButton( leading: Image.asset( Images.tab3, height: 24, width: 24, ), text: 'tab3'.tr, textStyle: AppTextStyle.addBtnTxt .copyWith(color: AppColor.primaryFontBlack), icon: null, ), GButton( leading: Image.asset( Images.tab4, height: 24, width: 24, ), text: 'tab4'.tr, textStyle: AppTextStyle.addBtnTxt .copyWith(color: AppColor.primaryFontBlack), icon: null, ), ], selectedIndex: _selectedIndex, onTabChange: (index) { if (mounted) setState(() { _selectedIndex = index; _onItemTapped(index); }); }, ), ), ), ), — Reply to this email directly, view it on GitHub <#75 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADGCEC2CGGW3Q2OT544LPNTVBHG2NANCNFSM5RK6EZ7A . You are receiving this because you commented.Message ID: @.***>

List<Widget> _pages;
  Widget _page1;
  Widget _page2;
  Widget _page3;
  Widget _page4;

  int _selectedIndex;
  Widget _currentPage;

  @override
  void initState() {
    super.initState();
    _page1 = const Tab1();
    _page2 = const Tab2();
    _page3 = const Tab3();
    _page4 = const Tab4();

    _pages = [_page1, _page2, _page3, _page4];

    _selectedIndex = 0;
    _currentPage = _page1;
  }

  void _onItemTapped(int index) {
    if (mounted)
      setState(() {
        _selectedIndex = index;
        _currentPage = _pages[index];
      });
  }

@sooxt98
Copy link
Owner

sooxt98 commented Mar 22, 2022 via email

@pradeep14598
Copy link
Author

pradeep14598 commented Mar 22, 2022

Could you just send me a complete file that is runnable? currentPage Is missing

On Tue, 22 Mar 2022 at 10:06 PM, Pk @.> wrote: I’m not able to run this, onItemTapped is missing … <#m_3194987528613587962_> On Tue, 22 Mar 2022 at 9:59 PM, Pk @.> wrote: could u provide a minimal reproducible code? … <#m_587440306533223400> On Tue, 22 Mar 2022 at 9:46 PM, Pk @.> wrote: E/flutter ( 7192): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: setState() called after dispose(): _GNavState#fb094(lifecycle state: defunct, not mounted) E/flutter ( 7192): This 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. E/flutter ( 7192): The 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. E/flutter ( 7192): This 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(). — Reply to this email directly, view it on GitHub <#75 <#75> <#75 <#75>>>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADGCEC7AHFHPEMJLAFM7D73VBHFJ7ANCNFSM5RK6EZ7A https://github.com/notifications/unsubscribe-auth/ADGCEC7AHFHPEMJLAFM7D73VBHFJ7ANCNFSM5RK6EZ7A https://github.com/notifications/unsubscribe-auth/ADGCEC7AHFHPEMJLAFM7D73VBHFJ7ANCNFSM5RK6EZ7A https://github.com/notifications/unsubscribe-auth/ADGCEC7AHFHPEMJLAFM7D73VBHFJ7ANCNFSM5RK6EZ7A . You are receiving this because you are subscribed to this thread.Message ID: @.> bottomNavigationBar: Container( // height: kBottomNavigationBarHeight, decoration: BoxDecoration( color: Colors.white, boxShadow: [ BoxShadow( blurRadius: 20, color: Colors.black.withOpacity(.1), ) ], ), child: SafeArea( child: Padding( padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 8), child: GNav( rippleColor: AppColor.surfaceDisabled1, hoverColor: AppColor.surfaceDisabled1, gap: 4, activeColor: AppColor.splash, iconSize: 24, padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6), tabBackgroundColor: AppColor.surfaceDisabled1, color: AppColor.surfaceDisabled1, tabs: [ GButton( leading: Image.asset( Images.tab1, height: 24, width: 24, ), text: 'tab1'.tr, textStyle: AppTextStyle.addBtnTxt .copyWith(color: AppColor.primaryFontBlack), icon: null, ), GButton( leading: Image.asset( Images.tab2, height: 24, width: 24, ), text: 'tab2'.tr, textStyle: AppTextStyle.addBtnTxt .copyWith(color: AppColor.primaryFontBlack), icon: null, ), GButton( leading: Image.asset( Images.tab3, height: 24, width: 24, ), text: 'tab3'.tr, textStyle: AppTextStyle.addBtnTxt .copyWith(color: AppColor.primaryFontBlack), icon: null, ), GButton( leading: Image.asset( Images.tab4, height: 24, width: 24, ), text: 'tab4'.tr, textStyle: AppTextStyle.addBtnTxt .copyWith(color: AppColor.primaryFontBlack), icon: null, ), ], selectedIndex: _selectedIndex, onTabChange: (index) { if (mounted) setState(() { _selectedIndex = index; _onItemTapped(index); }); }, ), ), ), ), — Reply to this email directly, view it on GitHub <#75 (comment) <#75 (comment)>>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADGCEC2CGGW3Q2OT544LPNTVBHG2NANCNFSM5RK6EZ7A . You are receiving this because you commented.Message ID: @.> void _onItemTapped(int index) { if (mounted) setState(() { _selectedIndex = index; _currentPage = _pages[index]; }); } — Reply to this email directly, view it on GitHub <#75 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADGCECZ6TAIFJT4VL642PZ3VBHHU3ANCNFSM5RK6EZ7A . You are receiving this because you commented.Message ID: @.>

https://gist.github.com/pradeep14598/b191d0bacd5ae9d67d1e6094152a4183

@sooxt98
Copy link
Owner

sooxt98 commented Mar 22, 2022

@pradeep14598 the DashboardController is missing, could you make it runnable first in one single file?

@pradeep14598
Copy link
Author

@pradeep14598 the DashboardController is missing, could you make it runnable first in one single file?

https://gist.github.com/pradeep14598/b191d0bacd5ae9d67d1e6094152a4183

@sooxt98
Copy link
Owner

sooxt98 commented Mar 22, 2022

@pradeep14598 you shouldnt put your final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>(); inside the build function

you should put at here

class Test extends StatefulWidget {
  final String? title;

  final GlobalKey<ScaffoldState> _scaffoldkey = new GlobalKey<ScaffoldState>();
  Test({@required this.title});

  @override
  _TestState createState() => _TestState();
}

class _TestState extends State<Test> {
  @override
  Widget build(BuildContext context) {
    
  }
}

@pradeep14598
Copy link
Author

@pradeep14598 you shouldnt put your final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>(); inside the build function

you should put at here

class Test extends StatefulWidget {
  final String? title;

  final GlobalKey<ScaffoldState> _scaffoldkey = new GlobalKey<ScaffoldState>();
  Test({@required this.title});

  @override
  _TestState createState() => _TestState();
}

class _TestState extends State<Test> {
  @override
  Widget build(BuildContext context) {
    
  }
}

Hai @sooxt98 ,Thanks for your solution and your awesome plugin ,is there any feature for adding badges in tab

@sooxt98
Copy link
Owner

sooxt98 commented Mar 22, 2022

@sooxt98 sooxt98 closed this as completed Mar 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants