Go Router + Riverpod Tutorial Series 5: Advanced Redirection with State Restoration

Search for a command to run...

In here we'll look at a series of tutorials covering Go Router and Riverpod together.
Now let's take a look at redirection depending on user roles Source code: role_based_redirection Now, Let's update our files starting with : AuthNotifier and authProvider: This is the updated AuthNotifier class AuthNotifier extends StateNotifier<Map<...
While I generally write about Flutter. This problem has been bugging me for a while and there seem to be limited resources to find a solution. So for anyone who is looking for a solution. Here you go: Type edge:flags into the address bar and open it...
In this tutorial, we’ll go through covering interceptor, which is a very crucial feature of Dio. You can find the entire source code for the project here: dio_tasker Understanding Interceptors: What is an interceptor? An interceptor in Dio is a powe...

In this part, we’ll enhance our Task Manager app by parsing JSON responses and handling different types of errors. We’ll use Dio’s built-in error handling mechanisms. You can find the source code of the entire app here: dio_tasker Parsing JSON Respon...

While building apps in Flutter is extremely fluid and wonderfully intuitive. We obviously know that we can’t build the next TikTok or Twitter by only building offline apps. Which is why we absolutely need internet capabilities in our Flutter apps. Th...

Understanding when to use setState in Flutter is crucial for managing our app’s state effectively. Here’s a detailed guide: When to use setState ? Updating the UI: Use setState when we need to update the UI in response to changes in the internal sta...

First let's update our AuthNotifier to support state restoration
Source code: redirection_with_state_restoration
First we are going to use flutter_secure_storage to store the state of the app.
So lets add it our pubspec.yaml
flutter pub add flutter_secure_storage
class AuthNotifier extends StateNotifier<Map<String, dynamic>> {
final FlutterSecureStorage _storage = const FlutterSecureStorage();
AuthNotifier() : super({'loggedIn': false, 'role': 'guest'}) {
_restoreState();
}
Future<void> _restoreState() async {
//First read the loggedIn state from the storage
final loggedIn = await _storage.read(key: 'loggedIn') == 'true';
//Next read the role from the storage
final role = await _storage.read(key: 'role') ?? 'guest';
state = {'loggedIn': loggedIn, 'role': role};
}
Future<void> login(String role) async{
state = {'loggedIn': true, 'role': role};
print('State change to true, Logged in as $role');
//Save the loggedIn state to the storage
await _storage.write(key: 'loggedIn', value: 'true');
await _storage.write(key: 'role', value: role);
}
Future<void> logout() async{
state = {'loggedIn': false, 'role': 'guest'};
print('State change to false, Logged out');
//Save the loggedOut state to the storage
await _storage.write(key: 'loggedIn', value: 'false');
await _storage.write(key: 'role', value: 'guest');
}
}
Now that we have added the state restoration. So whenever the user is logged in. Even if we close and reopen the app. The authentication system will remain the same.

As always you can find the source code for this tutorial here: redirection_with_state_restoration