---
title: "Discussion on \"Flutter Authentication Flow with Go Router and Provider\" | Hashnode"
description: "Discussion on \"Flutter Authentication Flow with Go Router and Provider\". Splash, Onboarding and much more"
url: "https://hashnode.com/posts/flutter-authentication-flow-with-go-router-and-provider/61c37acec0206b3f41f549c5"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Flutter Authentication Flow with Go Router and Provider](https://blog.ishangavidusha.com/flutter-authentication-flow-with-go-router-and-provider?utm_source=hashnode&utm_medium=feed)

Splash, Onboarding and much more

Dec 22, 2021 · 13 min read · blog.ishangavidusha.com  
Updated Jul 18, 2022  
Read the article

-   [#flutter-examples](https://hashnode.com/tag/flutter-examples)
-   [#flutter](https://hashnode.com/tag/flutter)
-   [#dart](https://hashnode.com/tag/dart)
-   [#authentication](https://hashnode.com/tag/authentication)

## Written by

[Ishanga Vidusha](https://hashnode.com/@ishanga)

@ishanga · Sri Lanka

  
[View profile](https://hashnode.com/@ishanga)

self-taught 👨🏻‍💻 | full-stack 💛

## Responses 6

Markdown supported · ⌘↵ to post

[Renato Dehnhardt](https://hashnode.com/@rawnato)

[Mar 2, 2024](https://hashnode.com/posts/flutter-authentication-flow-with-go-router-and-provider/61c37acec0206b3f41f549c5/comment/65e2ec3f202854fd6070fe16)

Hey man, nice tutorial, I really like the way that you create the authentication process! And I'm trying to do something like this. I'm got this error when try to execute the project!

```
unknown route name: /start
'package:go_router/src/configuration.dart':
Failed assertion: line 266 pos 12: '_nameToPath.containsKey(name)'
```

and everything it's is like the tutorial! Do you known if the problem are caused by some different version or something like this?

[Renato Dehnhardt](https://hashnode.com/@rawnato)

[Mar 2, 2024](https://hashnode.com/posts/flutter-authentication-flow-with-go-router-and-provider/61c37acec0206b3f41f549c5/comment/65e2ec3f202854fd6070fe16#reply-65e2f2669e0e76c546f652cb)

I found the problem, I think this is something related to new version of GoRouter or something like this.

Before

```
final loginLocation = state.namedLocation(APP_PAGE.login.toPath);
final homeLocation = state.namedLocation(APP_PAGE.home.toPath);
final splashLocation = state.namedLocation(APP_PAGE.splash.toPath);
final onboardLocation = state.namedLocation(APP_PAGE.onBoarding.toPath);
```

After

```
final onBoardLocation = state.namedLocation(APP_PAGE.onBoarding.toName);
final splashLocation = state.namedLocation(APP_PAGE.splash.toName);
final loginLocation = state.namedLocation(APP_PAGE.login.toName);
final homeLocation = state.namedLocation(APP_PAGE.home.toName);
```

But now there's this exception.

```
======== Exception caught by foundation library ====================================================
The following assertion was thrown while dispatching notifications for GoRouterDelegate:
setState() or markNeedsBuild() called during build.

This Router<Object> widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was: Router<Object>
  dependencies: [UnmanagedRestorationScope]
  state: _RouterState<Object>#2674b
The widget which was currently being built when the offending call was made was: Builder
When the exception was thrown, this was the stack: 
#0      Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:5042:9)
#1      Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:5054:6)
#2      State.setState (package:flutter/src/widgets/framework.dart:1223:15)
#3      _RouterState._handleRouterDelegateNotification (package:flutter/src/widgets/router.dart:796:5)
#4      ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:433:24)
#5      GoRouterDelegate._setCurrentConfiguration (package:go_router/src/delegate.dart:269:5)
#6      GoRouterDelegate.setNewRoutePath.<anonymous closure> (package:go_router/src/delegate.dart:231:18)
#7      SynchronousFuture.then (package:flutter/src/foundation/synchronous_future.dart:43:39)
#8      GoRouterDelegate.setNewRoutePath (package:go_router/src/delegate.dart:227:14)
#9      _RouterState._processParsedRouteInformation.<anonymous closure> (package:flutter/src/widgets/router.dart:758:34)
#10     SynchronousFuture.then (package:flutter/src/foundation/synchronous_future.dart:43:39)
#11     _RouterState._processRouteInformation (package:flutter/src/widgets/router.dart:750:8)
#12     _RouterState._handleRouteInformationProviderNotification (package:flutter/src/widgets/router.dart:767:5)
#13     ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:433:24)
#14     GoRouteInformationProvider.notifyListeners (package:go_router/src/information_provider.dart:134:11)
#15     ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:433:24)
#16     AppService.onAppStart (package:customers/services/app_service.dart:48:5)
#17     _SplashPageState.onStartUp (package:customers/pages/splash/page.dart:27:22)
#18     _SplashPageState.initState (package:customers/pages/splash/page.dart:21:5)
#19     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5611:55)
#20     ComponentElement.mount (package:flutter/src/widgets/framework.dart:5456:5)
...     Normal element mounting (226 frames)
#246    Element.inflateWidget (package:flutter/src/widgets/framework.dart:4335:16)
#247    MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6893:36)
#248    MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6905:32)
...     Normal element mounting (487 frames)
#735    _InheritedProviderScopeElement.mount (package:provider/src/inherited_provider.dart:411:11)
...     Normal element mounting (7 frames)
#742    SingleChildWidgetElementMixin.mount (package:nested/nested.dart:222:11)
...     Normal element mounting (7 frames)
#749    _NestedHookElement.mount (package:nested/nested.dart:187:11)
...     Normal element mounting (7 frames)
#756    _InheritedProviderScopeElement.mount (package:provider/src/inherited_provider.dart:411:11)
...     Normal element mounting (7 frames)
#763    SingleChildWidgetElementMixin.mount (package:nested/nested.dart:222:11)
...     Normal element mounting (7 frames)
#770    _NestedHookElement.mount (package:nested/nested.dart:187:11)
...     Normal element mounting (7 frames)
#777    _InheritedProviderScopeElement.mount (package:provider/src/inherited_provider.dart:411:11)
...     Normal element mounting (7 frames)
#784    SingleChildWidgetElementMixin.mount (package:nested/nested.dart:222:11)
...     Normal element mounting (7 frames)
#791    _NestedHookElement.mount (package:nested/nested.dart:187:11)
...     Normal element mounting (7 frames)
#798    SingleChildWidgetElementMixin.mount (package:nested/nested.dart:222:11)
...     Normal element mounting (35 frames)
#833    Element.inflateWidget (package:flutter/src/widgets/framework.dart:4335:16)
#834    Element.updateChild (package:flutter/src/widgets/framework.dart:3846:18)
#835    _RawViewElement._updateChild (package:flutter/src/widgets/view.dart:291:16)
#836    _RawViewElement.mount (package:flutter/src/widgets/view.dart:314:5)
...     Normal element mounting (7 frames)
#843    Element.inflateWidget (package:flutter/src/widgets/framework.dart:4335:16)
#844    Element.updateChild (package:flutter/src/widgets/framework.dart:3846:18)
#845    RootElement._rebuild (package:flutter/src/widgets/binding.dart:1354:16)
#846    RootElement.mount (package:flutter/src/widgets/binding.dart:1323:5)
#847    RootWidget.attach.<anonymous closure> (package:flutter/src/widgets/binding.dart:1276:18)
#848    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2844:19)
#849    RootWidget.attach (package:flutter/src/widgets/binding.dart:1275:13)
#850    WidgetsBinding.attachToBuildOwner (package:flutter/src/widgets/binding.dart:1088:27)
#851    WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:1070:5)
#852    WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:1056:7)
#856    _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
(elided 3 frames from class _Timer and dart:async-patch)
The GoRouterDelegate sending notification was: Instance of 'GoRouterDelegate'
====================================================================================================

======== Exception caught by foundation library ====================================================
The following assertion was thrown while dispatching notifications for AppService:
setState() or markNeedsBuild() called during build.

This _InheritedProviderScope<AppService?> widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was: _InheritedProviderScope<AppService?>
  value: Instance of 'AppService'
  listening to value
The widget which was currently being built when the offending call was made was: Builder
When the exception was thrown, this was the stack: 
#0      Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:5042:9)
#1      Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:5054:6)
#2      _InheritedProviderScopeElement.markNeedsNotifyDependents (package:provider/src/inherited_provider.dart:577:5)
#3      ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:433:24)
#4      AppService.onAppStart (package:customers/services/app_service.dart:48:5)
#5      _SplashPageState.onStartUp (package:customers/pages/splash/page.dart:27:22)
#6      _SplashPageState.initState (package:customers/pages/splash/page.dart:21:5)
#7      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5611:55)
#8      ComponentElement.mount (package:flutter/src/widgets/framework.dart:5456:5)
...     Normal element mounting (226 frames)
#234    Element.inflateWidget (package:flutter/src/widgets/framework.dart:4335:16)
#235    MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6893:36)
#236    MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6905:32)
...     Normal element mounting (487 frames)
#723    _InheritedProviderScopeElement.mount (package:provider/src/inherited_provider.dart:411:11)
...     Normal element mounting (7 frames)
#730    SingleChildWidgetElementMixin.mount (package:nested/nested.dart:222:11)
...     Normal element mounting (7 frames)
#737    _NestedHookElement.mount (package:nested/nested.dart:187:11)
...     Normal element mounting (7 frames)
#744    _InheritedProviderScopeElement.mount (package:provider/src/inherited_provider.dart:411:11)
...     Normal element mounting (7 frames)
#751    SingleChildWidgetElementMixin.mount (package:nested/nested.dart:222:11)
...     Normal element mounting (7 frames)
#758    _NestedHookElement.mount (package:nested/nested.dart:187:11)
...     Normal element mounting (7 frames)
#765    _InheritedProviderScopeElement.mount (package:provider/src/inherited_provider.dart:411:11)
...     Normal element mounting (7 frames)
#772    SingleChildWidgetElementMixin.mount (package:nested/nested.dart:222:11)
...     Normal element mounting (7 frames)
#779    _NestedHookElement.mount (package:nested/nested.dart:187:11)
...     Normal element mounting (7 frames)
#786    SingleChildWidgetElementMixin.mount (package:nested/nested.dart:222:11)
...     Normal element mounting (35 frames)
#821    Element.inflateWidget (package:flutter/src/widgets/framework.dart:4335:16)
#822    Element.updateChild (package:flutter/src/widgets/framework.dart:3846:18)
#823    _RawViewElement._updateChild (package:flutter/src/widgets/view.dart:291:16)
#824    _RawViewElement.mount (package:flutter/src/widgets/view.dart:314:5)
...     Normal element mounting (7 frames)
#831    Element.inflateWidget (package:flutter/src/widgets/framework.dart:4335:16)
#832    Element.updateChild (package:flutter/src/widgets/framework.dart:3846:18)
#833    RootElement._rebuild (package:flutter/src/widgets/binding.dart:1354:16)
#834    RootElement.mount (package:flutter/src/widgets/binding.dart:1323:5)
#835    RootWidget.attach.<anonymous closure> (package:flutter/src/widgets/binding.dart:1276:18)
#836    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2844:19)
#837    RootWidget.attach (package:flutter/src/widgets/binding.dart:1275:13)
#838    WidgetsBinding.attachToBuildOwner (package:flutter/src/widgets/binding.dart:1088:27)
#839    WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:1070:5)
#840    WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:1056:7)
#844    _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
(elided 3 frames from class _Timer and dart:async-patch)
The AppService sending notification was: Instance of 'AppService'
====================================================================================================
```

I'll keep looking for a solution, but if anyone knows and can help here in the comments, I'd be very grateful.

[rejeb haithem](https://hashnode.com/@Haithemrk)

[Dec 14, 2023](https://hashnode.com/posts/flutter-authentication-flow-with-go-router-and-provider/61c37acec0206b3f41f549c5/comment/657b634ad69ca11ab7883103)

Hi Ishan. Thank you for your article. I have a question, and need some help from you about go\_router. I am in situation that's i want to display a different statefulshell.indexedstack for every type of logged user example : normal user , admin, or super admin so there will be a different nav bar with a different branches if you can please help me or lead me to a solution i have done a try to distinct the branches but it's also only see the initial location and ignore the rest of the routes if there is any solution to change the initial location dynamicly gor every type of user.

[Vasant](https://hashnode.com/@vasantp20)

[Oct 21, 2023](https://hashnode.com/posts/flutter-authentication-flow-with-go-router-and-provider/61c37acec0206b3f41f549c5/comment/653369a91876d54fd5693e3c)

As I was going through docs for refreshListenable, it appears this is going to be depreciated. Reference: github/flutter/flutter/issues/116651

[Mauricio Ahumada](https://hashnode.com/@SonMauri)

[Nov 26, 2022](https://hashnode.com/posts/flutter-authentication-flow-with-go-router-and-provider/61c37acec0206b3f41f549c5/comment/63828840def1df04cdae9bca)

Hi, thanks for the tutorial. however I couldn't make it work. Cloned your repo and got some errors from the get go. I had to

```
flutter create .
```

and

```
flutter pub upgrade
```

Besides updating the dependencies in pubspec.yaml

```
  provider: ^6.0.4
  go_router: ^5.2.0
  shared_preferences: ^2.0.15
```

Also, I had to change the redirect definition to

```
redirect: (context, state) {
```

After all that I could finally run the app but only to find out it never leaves the splashScreen... All it shows it the CircularProgressIndicator forever...

In onAppStart() I commented out the line that waited for two seconds then I ran into an error I couldn't figure it out.

```
======== Exception caught by foundation library ====================================================
The following assertion was thrown while dispatching notifications for AppService:
setState() or markNeedsBuild() called during build.
```

I googled the error but failed miserable. Any ideas?

[Daewoom Kim](https://hashnode.com/@daepal)

[Dec 5, 2022](https://hashnode.com/posts/flutter-authentication-flow-with-go-router-and-provider/61c37acec0206b3f41f549c5/comment/63828840def1df04cdae9bca#reply-638d657c4a96bb8fda439f85)

Same here. I think the difference between Go\_Router versions causes the issue. When I run sample project, it works as expected.(2.5.5) However, when I updated go\_router version number to the latest (5.2.1), it shows progress indicator forever.

Any idea to solve or go around the issue?

[Valerii Igumentsev](https://hashnode.com/@booperdev)

[Jan 15, 2023](https://hashnode.com/posts/flutter-authentication-flow-with-go-router-and-provider/61c37acec0206b3f41f549c5/comment/63828840def1df04cdae9bca#reply-63c40a9b8f346b3f17ab91d3)

[Daewoom Kim](https://hashnode.com/@daepal)

Remove routeDelegare and routeInformationParser, and just provide goRouter object to routerConfig prop of MaterialApp.router constructor, like this:

routerConfig: goRouter,

[Khoshbin](https://hashnode.com/@xoshbin)

[Jul 9, 2023](https://hashnode.com/posts/flutter-authentication-flow-with-go-router-and-provider/61c37acec0206b3f41f549c5/comment/63828840def1df04cdae9bca#reply-64ab09fba25b1020144636e4)

[Valerii Igumentsev](https://hashnode.com/@booperdev) Thank you, I was struggling with that

[Jhoel Ramos](https://hashnode.com/@Washo)

[Oct 14, 2022](https://hashnode.com/posts/flutter-authentication-flow-with-go-router-and-provider/61c37acec0206b3f41f549c5/comment/6349d3d47b9ff55063441265)

Hi Ishan, thank you for the article. I love the global state handling you implemented for the app, I'll wait for the article using deep links. Could you explain about this same project for handling multiple streams?

[Buba](https://hashnode.com/@Buba)

[Jul 17, 2022](https://hashnode.com/posts/flutter-authentication-flow-with-go-router-and-provider/61c37acec0206b3f41f549c5/comment/62d3ee702f40e31decd8c94a)

Hi Ishanga, thank you for the article. Newbie here :D. Where should I keep the logged in user data? Should I keep it in the AppService provider or in the AuthService? Sorry just trying to learn flutter and figure out what is going on.

[Ishanga Vidusha](https://hashnode.com/@ishanga)

[Jul 17, 2022](https://hashnode.com/posts/flutter-authentication-flow-with-go-router-and-provider/61c37acec0206b3f41f549c5/comment/62d3ee702f40e31decd8c94a#reply-62d3f77a2f40e31decd8ca7c)

It’s better to use AuthService for keep the user data. You probably need to make variable for it and change Stream also to that type. And add some function to save and read user data from storage.

[Buba](https://hashnode.com/@Buba)

[Jul 17, 2022](https://hashnode.com/posts/flutter-authentication-flow-with-go-router-and-provider/61c37acec0206b3f41f549c5/comment/62d3ee702f40e31decd8c94a#reply-62d3f8ee2f40e31decd8caa8)

[Ishanga Vidusha](https://hashnode.com/@ishanga) Thank you so much.

[Elijah Yannick](https://hashnode.com/@elijahyannick)

[Jun 17, 2022](https://hashnode.com/posts/flutter-authentication-flow-with-go-router-and-provider/61c37acec0206b3f41f549c5/comment/62ac4a361a89c91850b1cfa3)

Hi Ishan. Thank you for your article. I have a question, and need some help from you about go\_router.

First I want to know if the refresh works perfectly on web with your implementation. ? I'm working on a web app, with flutter. When I do a browser refresh I'm redirected to login page, and the login does not work again.

[Ishanga Vidusha](https://hashnode.com/@ishanga)

[Jun 17, 2022](https://hashnode.com/posts/flutter-authentication-flow-with-go-router-and-provider/61c37acec0206b3f41f549c5/comment/62ac4a361a89c91850b1cfa3#reply-62ac4e0e1a89c91850b1d066)

GoRouter perfectly works on Flutter web. To keep the login state on browser refresh you should save the state in some persistent storage and get it on app start perhaps you can use splash screen for it. In real-world applications people use may local storage to store access tokens or something to validate the login state.

[Jiten Basnet](https://hashnode.com/@GtenSoftware)

[Mar 6, 2022](https://hashnode.com/posts/flutter-authentication-flow-with-go-router-and-provider/61c37acec0206b3f41f549c5/comment/6224252ab9cb874b840ca7f6)

Hi Ishan, Thankyou for the article. It was very useful to me. One question i would like to ask though! You have passed the bloc or provider globally above materialApp here? What about when I need to pass the bloc inside separate routes or sub routes.

[Ishanga Vidusha](https://hashnode.com/@ishanga)

[Mar 17, 2022](https://hashnode.com/posts/flutter-authentication-flow-with-go-router-and-provider/61c37acec0206b3f41f549c5/comment/6224252ab9cb874b840ca7f6#reply-623315d8da27a31721c8878c)

Sorry for the late reply. I think you can do it using the navigatorBuilder function in the GoRouter. you can get more information from go router documentation. [gorouter.dev/navigator-builder](https://gorouter.dev/navigator-builder)
