context.go vs. context.pushNamed

지니🧸·2024년 2월 21일
0

Flutter

목록 보기
5/7

context.go

  • Package/Library: This method is typically associated with the go_router package, or similar routing packages, that provide a more declarative approach to routing in Flutter apps.
  • Usage: to navigate to a route based on the path you specify
    • replaces the current route with the new route, changing the URL in web applications
    • does not add the new route on top of the navigation stack.
  • Behavior: When you use context.go('/path'), you are instructing the app to navigate to a specific route and replace the current route in the navigator's stack. It's more about changing the state of the navigation to show a different screen based on a new path.
  • Suitable for: web and deep linking scenarios where the navigation state needs to sync with a URI.

context.pushNamed

- Package/Library: This is a method from Flutter's built-in Navigator class, part of the material library.

  • Usage: Used with named routes,
    • pushes a new route onto the navigator's stack
    • requires you to have defined the routes in advance in your MaterialApp or CupertinoApp widget.
  • Behavior: adds the new route on top of the navigation stack, allowing for a back navigation to the previous route.
    • akin to navigating forward to a new screen while keeping the history.
  • Suitable for: This is suitable for traditional app navigation where you want to maintain a stack of screens and provide the ability for the user to navigate back through the stack.

Key Differences

  • Navigation Stack Management:
    • context.pushNamed adds a route on top of the stack, allowing for back navigation.
    • context.go changes the current route to a new one, which is more about changing the state to reflect a new path, not stacking.
  • Usage Context:
    • context.go is more aligned with web and deep link navigation patterns, often used with packages like go_router.
    • context.pushNamed is part of Flutter's default navigation system, ideal for mobile apps that require a stack-based navigation.
  • Package Dependency:
    • context.go is not part of the Flutter's core navigation API and requires a specific package that implements this function, like go_router.
    • context.pushNamed is available by default with Flutter's Navigator class.
profile
우당탕탕

0개의 댓글