{"id":27721,"date":"2022-06-14T12:42:44","date_gmt":"2022-06-14T12:42:44","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/blog\/?p=27721"},"modified":"2025-11-13T07:25:25","modified_gmt":"2025-11-13T07:25:25","slug":"flutter-navigator-2-0","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/blog\/flutter-navigator-2-0","title":{"rendered":"Basic Routing with Flutter Navigator 2.0 Example"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>Navigator 2.0 was introduced by the Flutter team to handle complex routing Navigator 1.0, it was simply adding and removing the pages from stacks that were very complex for handling <strong>deep linking<\/strong> or <b>URI parsing for Flutter Web<\/b> hence Navigator 2.0 to the rescue or is it?.<\/p>\n<h2>Flutter Navigator 2.0: What and Why?<\/h2>\n<p>Navigator 2.0 is a declarative API created by flutter as opposed to Navigator 1.0, an Imperative API, though, at root, it also works in an imperative style.<\/p>\n<h3>Limitation of Navigator 1.0<\/h3>\n<p>In navigator 1.0, we were provided a stack to which we could PUSH or POP pages which was pretty simple and convenient until our application had simple routing that is few screens and didn\u2019t need to be used on the web.<\/p>\n<p>The problem started when we were required to make complex applications, implementing \u2018deep linking,\u2019 directly going to a product page from notifications, O.S events handling, and URI parsing for Flutter web back button handling.<\/p>\n<h3>Why Flutter Navigator 2.0?<\/h3>\n<p>Then came Navigator 2.0, which is declarative, i.e., you get more control over the Navigation stack; using nav 2.0, we control the stack as per our app state and handle back button events fired by O.S. Using this, we do not only can PUSH and POP from the top, but from anywhere we wanted to by providing a list of pages and URI parsing by using routeInformationParser and provider.<\/p>\n<h2>Tutorial Goal<\/h2>\n<p>In our tutorial, we are going to build a simple demo application consisting of two screens:<\/p>\n<ul class=\"bullets text-left\">\n<li><strong>Screen 1 for Anime Characters:<\/strong> It is going to show a list of anime characters.<\/li>\n<li><strong>Screen 2 for Character Details:<\/strong> It is connected to screen 1 via callback function and shows detail about the selected character from screen 1.<\/li>\n<\/ul>\n<p><strong>Goal:<\/strong> To demonstrate the working of Navigator 2.0, i.e., changing screens based on our application state <mark>(selectedChar != null)<\/mark> without using simple POP &#038; PUSH and by passing our stack of pages to MaterialApp via Navigator<\/p>\n<p class=\"boxed bg--secondary\" style=\"border: 1px solid #c7c7c7; box-shadow: 0 0 40px rgba(0, 0, 0, 0.2);\"><strong><i><span style=\"font-size:22px; color:#000;\">Developing a Flutter application is easier and hustle-free with Bacancy!<\/span><br \/>\nAre you looking for top-notch Flutter developers for your requirements? Trust our expertise! Contact us today and hire <a href=\"https:\/\/www.bacancytechnology.com\/hire-flutter-developer\" target=\"_blank\" rel=\"noopener\">Flutter developer<\/a>.<\/i><\/strong><\/p>\n<h2>Flutter Navigator 2.0 Example<\/h2>\n<p>Navigator 2.0 adds extra classes to implement the app screen as a product of the app state and to be able to parse routes from URLs for flutter web. Navigator 2.0 provides a new declarative API design, i.e.,<\/p>\n<ul class=\"bullets text-left\">\n<li><strong>Pages:<\/strong> Pages are for existing Navigator. Immutable pages that are used to set Navigator history stack. It describes the pages that need to be placed in the navigator history stack.<\/li>\n<li><strong>Router:<\/strong> It is a new widget that wraps the navigator and configures its current list of pages per app state. It can also listen to events from the operating system and configure the navigator accordingly.<\/li>\n<\/ul>\n<p>In our example: Flutter Navigator 2.0, we will basically develop a Flutter application and learn how to add pages and routers.<\/p>\n<p>There are four classes as mentioned below:<\/p>\n<ul class=\"bullets text-left\">\n<li>RouterInformationParser<\/li>\n<li>RouterDelegate<\/li>\n<li>RouterInformationProvider<\/li>\n<li>BackButtonDispatcher<\/li>\n<\/ul>\n<p>We will talk about them in another blog as it will create unnecessary confusion. For now, we will keep this tutorial simple but assure to write about them in the future in detail.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/II-1.jpg\" alt=\"Flutter Navigator 2.0 Program Flow\" width=\"1100\" height=\"676\" class=\"aligncenter size-full wp-image-27746\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/II-1.jpg 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/II-1-300x184.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/II-1-1024x629.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/II-1-768x472.jpg 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p>These are the classes that we will be working with.<\/p>\n<ul class=\"bullets text-left\">\n<li><strong>Anime_Characters screen:<\/strong> It shows the list of anime characters.<\/li>\n<li><strong>Characters_Details screen:<\/strong> On selecting particular character, a new screen is pushed onto the stack and displays the anime to which that character belongs.<\/li>\n<li><strong>Anime_Char_Model:<\/strong> It is just a model class for anime characters, with name, anime, and a constructor.<\/li>\n<li><strong>App class:<\/strong> It is a simple class responsible for Material widget and handling all navigating and tracking app state accordingly.<\/li>\n<\/ul>\n<p>Quick Read: <a href=\"https:\/\/www.bacancytechnology.com\/blog\/authentication-using-aws-amplify-in-flutter\" target=\"_blank\" rel=\"noopener\">Integrate Authentication using AWS Amplify in Flutter<\/a><\/p>\n<p>Now let\u2019s set up our pages inside app state. After that, create a variable to track application state and further display the screen as per state.<\/p>\n<p>https:\/\/gist.github.com\/MukulKrSingh\/0cfe0dddc39d718e303fb5bbe0c1544d<\/p>\n<pre>\/\/ ignore_for_file: dead_code, prefer_const_constructors\r\n\r\nimport 'package:flutter\/material.dart';\r\nimport 'package:navigator_2\/Models\/anime_char_model.dart';\r\nimport 'package:navigator_2\/Screens\/anime_characters.dart';\r\nimport 'package:navigator_2\/customRoute.dart';\r\n\r\nclass MyApp extends StatefulWidget {\r\n  const MyApp({Key? key}) : super(key: key);\r\n\r\n  @override\r\n  State<MyApp> createState() => _MyAppState();\r\n}\r\n\r\nclass _MyAppState extends State<MyApp> {\r\n  \/\/AnimeCharModel? _selectedChar = AnimeCharModel('Escanor', '7 deadly sins');\r\n  AnimeCharModel? _selectedChar;\r\n  \/\/bool unknown = false;\r\n\r\n  List<AnimeCharModel> animeChars = [\r\n    AnimeCharModel('Hashirama', 'Naruto'),\r\n    AnimeCharModel('Madara', 'Naruto'),\r\n    AnimeCharModel('Gojo', 'Jujutsu Kaisen'),\r\n    AnimeCharModel('Zoro', 'One Piece')\r\n  ];\r\n  void onCharTapped(AnimeCharModel selectedChar) {\r\n    setState(() {\r\n      _selectedChar = selectedChar;\r\n      \/\/print(selectedChar.anime);\r\n    });\r\n  }\r\n  @override\r\n  Widget build(BuildContext context) {\r\n    return MaterialApp(\r\n      title: 'Anime Characters',\r\n      home: Navigator(\r\n        \/\/ ignore: prefer_const_literals_to_create_immutables\r\n        pages: [\r\n          MaterialPage(\r\n            key: ValueKey('anime_chars'),\r\n            child: AnimeChars(\r\n              animeChar: animeChars,\r\n              onTapped: onCharTapped,\r\n            ),\r\n          ),\r\n          if (_selectedChar != null) CustomRoute(character: _selectedChar)\r\n        ],\r\n        \r\n        onPopPage: (route, result) {\r\n          if (!route.didPop(result)) {\r\n            return false;\r\n          }\r\n          setState(\r\n            () {\r\n              _selectedChar = null;\r\n            },\r\n          );\r\n          return true;\r\n        },\r\n      ),\r\n    );\r\n  }\r\n}<\/pre>\n<p>Here you can see that we have set up our pages by passing MaterialPage as children for each page for the custom routing. Just to show that we can also configure pages as per our needs I have created a customRoute.dart for Character_details screen just for example.<\/p>\n<p><strong>Page:<\/strong> We will use the key to <strong>uniquely identify each page.<\/strong><\/p>\n<p><strong>onPopPage:<\/strong>  The use of onPopPage is to ensure we do not into any unexpected state while POP is executed. When a POP operation is successful it returns true else false.<\/p>\n<p><strong>List<> animeChars:<\/strong> It holds a list of anime characters that is being passed to Anime_characters_screen.<\/p>\n<p><strong>_selectedChar:<\/strong> It is used to keep track of the application\u2019s state so as to display the character_details screen.<\/p>\n<p>Thus, this was about implementing Navigator 2.0 in our Flutter application.<\/p>\n<h2>Github Repository: Flutter Navigator 2.0 Example<\/h2>\n<p>So far, we have mentioned the core implementation of Navigator 2.0. The rest of the code is just connectivity between all the classes. For the entire application, you can clone the source code and feel free to play around with it.<\/p>\n<p>There is a google dev fest for the announcement of Navigator 2.0 you can check that out too<br \/>\nDev Fest GDG Saudi Arabia by Chun-heng-Tai.<\/p>\n<h2>What\u2019s More in Navigator 2.0?<\/h2>\n<p>Till now we have just barely scratched the surface that has just started to manage routes declaratively that are dependent on app states and not telling it ourselves to do. Now what\u2019s left is mostly <strong>the flutter web part that is parsing the URL to go to certain pages directly<\/strong> and vice-versa.<\/p>\n<p>To do the leftover part we will have provided you with an architecture used by the Flutter team to explain Navigator 2.0. You can study it and understand what is happening so that in our next blog\/video it would help to understand more. Even if you don\u2019t go through it thoroughly, no issues we will surely cover it in depth.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/II-1.jpg\" alt=\"Flutter Navigator 2.0 Program Flow\" width=\"1100\" height=\"676\" class=\"aligncenter size-full wp-image-27746\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/II-1.jpg 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/II-1-300x184.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/II-1-1024x629.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/II-1-768x472.jpg 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<h2>Conclusion<\/h2>\n<p>So, this was all about Flutter Navigator 2.0 and how to implement basic routing with Navigator 2.0 in your app. We hope the tutorial will help you get started with implementing navigation in the application. If you are a Flutter enthusiast, then the <a href=\"https:\/\/www.bacancytechnology.com\/tutorials\/flutter\" target=\"_blank\" rel=\"noopener\">Flutter tutorials<\/a> page is for you. We have got a github repository that you can clone and explore more about Flutter.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Navigator 2.0 was introduced by the Flutter team to handle complex routing Navigator 1.0, it was simply adding and removing the pages from stacks that were very complex for handling deep linking or URI parsing for Flutter Web hence Navigator 2.0 to the rescue or is it?. Flutter Navigator 2.0: What and Why? Navigator [&hellip;]<\/p>\n","protected":false},"author":171,"featured_media":27748,"comment_status":"open","ping_status":"open","sticky":false,"template":"blog-new-template.php","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"_lmt_disableupdate":"no","_lmt_disable":"","footnotes":""},"categories":[1200],"tags":[],"coauthors":[2425],"class_list":["post-27721","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-flutter"],"acf":[],"modified_by":"Nisarg Bhavsar","_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/27721","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/users\/171"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/comments?post=27721"}],"version-history":[{"count":3,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/27721\/revisions"}],"predecessor-version":[{"id":56546,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/27721\/revisions\/56546"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/media\/27748"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/media?parent=27721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/categories?post=27721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/tags?post=27721"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/coauthors?post=27721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}