What is the difference between using navigator 1.0 vs navigator 2.0 ? why flutter brought navigator 2.0 ?
#flutter
Answer
Navigator 1.0 vs Navigator 2.0
Navigator versions differ in complexity and control:
Navigator 1.0
Simple, imperative navigation:
dart// Push route Navigator.of(context).push(MaterialPageRoute( builder: (_) => DetailScreen(), )); // Pop route Navigator.of(context).pop(); // Push named route Navigator.of(context).pushNamed('/detail');
Pros:
- Simple and intuitive
- Easy to learn
- Great for small apps
Cons:
- Limited deep linking
- Hard to handle back button
- State not preserved
- Difficult for complex flows
Navigator 2.0
Complex, declarative navigation:
dartclass MyNavigator extends StatefulWidget { _MyNavigatorState createState() => _MyNavigatorState(); } class _MyNavigatorState extends State<MyNavigator> { Widget build(BuildContext context) { return WillPopScope( onWillPop: () async => !await _navigatorKey.currentState!.maybePop(), child: Navigator( pages: _pages, onPopPage: (route, result) { if (!route.didPop(result)) return false; _removeLastPage(); return true; }, ), ); } }
Pros:
- Full control over routes
- Deep linking support
- Browser back button handling
- Complex animations
- URL sync (web)
Cons:
- Complex boilerplate
- Steep learning curve
- Verbose code
Comparison
| Feature | 1.0 | 2.0 |
|---|---|---|
| Learning | Easy | Hard |
| Code | Less | More |
| Deep Links | Limited | Full |
| Web URL Sync | No | Yes |
| Back Button | Basic | Full Control |
Recommendation
- Nav 1.0: Small/medium apps, simple flows
- Nav 2.0: Large apps, complex flows, web apps, deep linking
- Alternative: Use go_router (easier 2.0)
Modern: go_router simplifies Navigator 2.0.