Go Router + Riverpod Tutorial Series 1: Basic Redirection

Search for a command to run...

No comments yet. Be the first to comment.
In here we'll look at a series of tutorials covering Go Router and Riverpod together.
In our previous article we have covered basic redirect. Now let's take a look at Conditional Redirection. What are Guards ? Guards: Guards are essentially functions that you can define to determine whether a particular redirection should happen or no...
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...

Redirection is one of the best features of Go Router. So in total we are going to look at 5 tutorials which explore redirection in increments of complexity. This article marks our first tutorial.
AuthNotifier and authProvider using RiverpodAuthNotifier and authProvider:
import 'package:flutter_riverpod/flutter_riverpod.dart';
class AuthNotifier extends StateNotifier<bool> {
AuthNotifier() : super(false);
void login() {
state = true;
}
void logout() {
state = false;
}
}
final authProvider = StateNotifierProvider<AuthNotifier, bool>((ref) {
return AuthNotifier();
});
HomePage:
class HomePage extends ConsumerWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
return Scaffold(
appBar: AppBar(
title: const Text('GRT Redirect'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('Home Page'),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () {
ref.read(authProvider.notifier).logout();
},
child: const Text('Logout')),
],
),
),
);
}
}
When the Logout button is pressed. The value of state change to false.
LoginPage:
class LoginPage extends ConsumerWidget {
const LoginPage({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
return Scaffold(
appBar: AppBar(
title: const Text('GRT Login'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('This is Login Page'),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () {
ref.read(authProvider.notifier).login();
},
child: const Text('Login')),
],
),
),
);
}
}
When Login Button is pressed. The value of state changes to true.
I have defined a routerProvider that provides us with our Go Router
final routerProvider = Provider<GoRouter>((ref) {
return GoRouter(
initialLocation: '/home',
routes: [
GoRoute(path: '/home', builder: (context, state) => const HomePage()),
GoRoute(path: '/login', builder: (context, state) => const LoginPage()),
],
redirect: (context, state) {
final loggedIn = ref.watch(authProvider);
// Checking to make sure that the current path is not Login
final isLoggingIn = state.path == '/login';
if (!loggedIn && !isLoggingIn) {
return '/login';
} else if(loggedIn && isLoggingIn) {
return '/home';
}
return null;
});
});
And finally here's how we connect this routerProvider to our App:
void main() {
runApp( const ProviderScope(child: MyApp()));
}
class MyApp extends ConsumerWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context, WidgetRef ref){
final router = ref.watch(routerProvider);
return MaterialApp.router(
title: 'GRT Redirect',
routerConfig: router,
);
}
}
Since the initial value of the state is false. Here's the output when I run the app:

Now when we tap on the login button. The state changes to true. And it'll take us to the home page.
