sooxt98 / google_nav_bar

A modern google style nav bar for flutter.
MIT License
735 stars 114 forks source link

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

Open realapril opened 2 years ago

realapril commented 2 years ago

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 commented 2 years ago

Could you provide a minimal code that produce this error?

Blisston commented 2 years ago

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 commented 2 years ago

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

alexandranb1 commented 2 years ago

It does the same issue on onPressed function

sooxt98 commented 2 years ago

Could you provide a minimal code that produce this error?

On Fri, 27 May 2022 at 8:59 PM, alexandranb1 @.***> wrote:

It does the same issue on onPressed function

— Reply to this email directly, view it on GitHub https://github.com/sooxt98/google_nav_bar/issues/69#issuecomment-1139595350, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADGCEC6II2RAFMZM6XQF7VLVMDBJNANCNFSM5GIMQT5Q . You are receiving this because you commented.Message ID: @.***>

alexandranb1 commented 2 years ago

[√] 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 commented 2 years ago

Hi there, I need the full code all in one file, at least it’s runnable, and able to reproduce the same error

On Fri, 27 May 2022 at 9:09 PM, alexandranb1 @.***> wrote:

[√] 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())); } ),

— Reply to this email directly, view it on GitHub https://github.com/sooxt98/google_nav_bar/issues/69#issuecomment-1139603738, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADGCEC57SDPCFJLLLS36RX3VMDCSFANCNFSM5GIMQT5Q . You are receiving this because you commented.Message ID: @.***>

sirfnomi commented 1 year ago
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 commented 1 year ago

@sooxt98 this code can give you exceptions