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

Navigator.of(context).pushReplacementNamed occurs Error #69

Open
realapril opened this issue Oct 19, 2021 · 9 comments
Open

Navigator.of(context).pushReplacementNamed occurs Error #69

realapril opened this issue Oct 19, 2021 · 9 comments

Comments

@realapril
Copy link

realapril commented Oct 19, 2021

First of all, I love your designed navigation widget.
However there is a minor error when I use Navigator function Navigator.of(context).pushReplacementNamed,
this error occurs.
Using Navigator.of(context).pushNamed is fine.

E/flutter ( 9134): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: setState() called after dispose(): _GNavState#38384(lifecycle state: defunct, not mounted)
E/flutter ( 9134): 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 ( 9134): 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 ( 9134): 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().
E/flutter ( 9134): #0      State.setState.<anonymous closure> (package:flutter/src/widgets/framework.dart:1052:9)
E/flutter ( 9134): #1      State.setState (package:flutter/src/widgets/framework.dart:1087:6)
E/flutter ( 9134): #2      _GNavState.build.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:google_nav_bar/src/gnav.dart:131:27)
E/flutter ( 9134): #3      new Future.delayed.<anonymous closure> (dart:async/future.dart:315:39)
E/flutter ( 9134): #4      _rootRun (dart:async/zone.dart:1420:47)
E/flutter ( 9134): #5      _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter ( 9134): #6      _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
E/flutter ( 9134): #7      _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1276:23)
E/flutter ( 9134): #8      _rootRun (dart:async/zone.dart:1428:13)
E/flutter ( 9134): #9      _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter ( 9134): #10     _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1260:23)
E/flutter ( 9134): #11     Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
E/flutter ( 9134): #12     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:395:19)
E/flutter ( 9134): #13     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:426:5)
E/flutter ( 9134): #14     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
E/flutter ( 9134): 

It doesn't kill an application so it might be a minor problem for now,
but I think it will be a pain later.

@sooxt98
Copy link
Owner

sooxt98 commented Oct 20, 2021

Could you provide a minimal code that produce this error?

@Blisston
Copy link

Blisston commented Jan 3, 2022

While using Navigator.of(context).popAndPushNamed(); inside the onTabChnage() callback throws the above error please check this. The same works fine if pushNamed is been used. It's a common case where developers can try to navigate to another page on tab change right? Correct me if i am using it wrong

@sooxt98
Copy link
Owner

sooxt98 commented Jan 11, 2022

@Blisston you could use GButton onPressed for Navigator, onTabChnage usually is used for setState to change tab content

@alexandranb1
Copy link

It does the same issue on onPressed function

@sooxt98
Copy link
Owner

sooxt98 commented May 27, 2022 via email

@alexandranb1
Copy link

[√] Flutter (Channel stable, 2.10.3, on Microsoft Windows [Version 10.0.19043.1706], locale en-US)
• Flutter version 2.10.3
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 7e9793dee1 (3 months ago), 2022-03-02 11:23:12 -0600
• Engine revision bd539267b4
• Dart version 2.16.1
• DevTools version 2.9.2

[√] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
And I have a Stateful widget, and in the body I have the clasic GNav and all of the tabs look like this :
GButton(
icon: Icons.perm_identity_sharp,
text: 'Profile',
onPressed: () {
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (context) => const Profile()));
}
),

@sooxt98
Copy link
Owner

sooxt98 commented May 27, 2022 via email

@sirfnomi
Copy link

sirfnomi commented Feb 20, 2023

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: const NavigationError(),
    );
  }
}

class NavigationError extends StatelessWidget {
  const NavigationError({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    int selectedIndex = 0;
    return Scaffold(
      body: Center(child: Text('Home')),
      bottomNavigationBar: Container(
        decoration: BoxDecoration(
          color: Color(0xFFDFDFDF),
          boxShadow: [
            BoxShadow(
              blurRadius: 20,
              color: Colors.black.withOpacity(.1),
            )
          ],
        ),
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 100.0, vertical: 10),
          child: GNav(
            rippleColor: Colors.grey[300]!,
            hoverColor: Colors.grey[100]!,
            gap: 1,
            activeColor: Colors.black,
            iconSize: 24,
            padding: EdgeInsets.symmetric(horizontal: 20, vertical: 12),
            duration: Duration(milliseconds: 500),
            tabBackgroundColor: Theme.of(context).colorScheme.surface,
            color: Colors.black,
            tabs: [
              GButton(
                icon: Icons.home,
                text: 'Home',
                onPressed: () {
                  Navigator.pushReplacement(
                      context,
                      PageRouteBuilder(
                          pageBuilder: (context, animation1, animation2) =>
                          const DashboardScreen(),
                          transitionDuration: Duration.zero,
                          reverseTransitionDuration: Duration.zero));
                },
              ),
              GButton(
                icon: Icons.person,
                text: 'Profile',
                onPressed: (){
                  Navigator.pushReplacement(
                      context,
                      PageRouteBuilder(
                          pageBuilder: (context, animation1, animation2) =>
                          const ProfileScreen(),
                          transitionDuration: Duration.zero,
                          reverseTransitionDuration: Duration.zero));
                },
              ),
            ],
            selectedIndex: selectedIndex,
          ),
        ),
      ),
    );
  }
}

@sirfnomi
Copy link

sirfnomi commented Mar 8, 2023

@sooxt98 this code can give you exceptions

18601673727 added a commit to 18601673727/google_nav_bar that referenced this issue May 13, 2023
18601673727 added a commit to 18601673727/google_nav_bar that referenced this issue May 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants