Comments (10)
Thank you, that indeed works. I originaly did not want to go with context.go
because I wanted to replace the page and not create a history entry. But I found out that there is Router.neglect
for that so using context.go
is sufficient for me. But I think I do not fully understand the purpose of context.replace
and context.pushReplacement
then. For me these methods are basically just breaking the navigation state.
from flutter.
@iSaqibShafique
Please provide complete runnable reproducible code sample without third party package implementation.
You may check similar issues reported for the package and see if they resemble your case or not:
from flutter.
Same issue for me
from flutter.
I guess there's an issue here on your redirect method for splash where you're returning null,
instead you can use this below code for redirect and check if it helps:
redirect: (context, state) { User? user = FirebaseAuthService().currentuser.toValueNotifier().value; if (user != null && user.emailVerified) { return maindashboardview; } else if (user != null && user.emailVerified == false) { return emailnotverified; } else { return login; }
If it doesn't then I think you have the same name and path which is not the case in go router, a path contains "/" but the name shouldn't, you might need to tweak that as well for e.g.
GoRoute(
path: "/login",
name: "login",
builder: (context, state) => const LoginView(),
),
Try to follow these two steps if it doesn't help then share the code you're using for navigation as well.
from flutter.
Hello @Mashood97 @darshankawar
Thanks for your inputs. I created a minimal reproduction where my problem occurs.
Here is the link to the reproduction repository: https://github.com/daviddomkar/go-router-location-empty-reproduction
More details and description of the problem:
I think the issue is with context.replace
on a top level route. In my example there are 3 routes:
- home route ('/')
- login route ('/auth/login')
- signup route ('/auth/signup')
I have setup my state using a ChangeNotifier
with a boolean which tells if the user is authenticated or not. I use this ChangeNotifier
as a refreshListenable
on the GoRouter
.
I have then setup the redirects in such a way that authenticated user is always redirected to home route and unauthenticated user is redirected to login route, from which he can go to signup route via a context.replace('/auth/signup')
, he can also go back to login route using context.replace('/auth/login')
, any other form of routing is handled by the refreshListenable
using signUp()
, logIn()
and logOut()
methods.
The issue is when I launch the app (state is unauthenticated) and I am on the login route. Then I press "Go to Sign Up" button. I am redirected via context.replace('/auth/signup')
to the signup route and when I click on the "Sign Up" button which calls signUp()
state method which triggers a redirect using refreshListenable
the exception "GoException (GoException: Location cannot be empty.)" is thrown.
I would instead assume the correct behavior to be to redirect me to the home route.
from flutter.
Yeah that's because you are directly handling every case inside each GoRoute redirect method. I would recommend you to use redirect method directly inside the GoRouter constructor. As per my understanding this below condition might work for you:
First define pre auth routes in a list as routes.
List<String> routes = [
"/auth/signup",
"/auth/login"
];
redirect: (ctx, state) async {
// check if you are authenticated with your auth state class.
final status = _state.isAuthenticated;
final loggedIn = status;
// Check if your logging in i.e on pre auth routes.
final logging = routes.contains(state.matchedLocation);
if (!loggedIn && !logging) return "/auth/login";
if (loggedIn && logging) return "/";
return null;
},
//Set initial location
initialLocation: _state.isAuthenticated
? "/";
: "/auth/login",
I guess this might work for you in this case, Please try and let me know if it helps.
from flutter.
Thanks for the reply. Unfortunately, I am still experiencing the exception even with your suggestion. I have updated my reproduction repository if you would like to try it.
from flutter.
I had same issue, I get my listenable from getIt, like this
refreshListenable : getIt<AuthViewModel>().authListenable
------ Error location cannot be empty
refreshListenable : getIt.get<AuthViewModel>().authListenable
------ NO ERRORS
so I guess it is something to do with how you get the object
from flutter.
I ran your code provided on repository, I made some couple of changes in code can you use this below code:
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
class AuthState extends ChangeNotifier {
bool _isAuthenticated = false;
bool get isAuthenticated => _isAuthenticated;
void signUp() {
_isAuthenticated = true;
notifyListeners();
}
void logIn() {
_isAuthenticated = true;
notifyListeners();
}
void logOut() {
_isAuthenticated = false;
notifyListeners();
}
}
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late final AuthState _state;
late final GoRouter _router;
List<String> routes = ["/auth/signup", "/auth/login"];
@override
void initState() {
super.initState();
_state = AuthState();
_router = GoRouter(
refreshListenable: _state,
initialLocation: _state.isAuthenticated ? "/" : "/auth/login",
redirect: (context, state) {
final status = _state.isAuthenticated;
final loggedIn = status;
final logging = routes.contains(state.matchedLocation);
if (!loggedIn && !logging) return "/auth/login";
if (loggedIn && logging) return "/";
return null;
},
routes: [
GoRoute(
path: '/',
builder: (context, state) => MyHomePage(
state: _state,
),
),
GoRoute(
path: '/auth/signup',
builder: (context, state) => MySignupPage(
state: _state,
),
),
GoRoute(
path: '/auth/login',
builder: (context, state) => MyLoginPage(
state: _state,
),
),
],
);
}
@override
void dispose() {
_router.dispose();
_state.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp.router(
title: 'Go Router Location Empty Exception Reproduction',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
routerConfig: _router,
);
}
}
class MyHomePage extends StatelessWidget {
final AuthState state;
const MyHomePage({
super.key,
required this.state,
});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () {
state.logOut();
},
child: const Text('Log out'),
),
),
);
}
}
class MyLoginPage extends StatelessWidget {
const MyLoginPage({
super.key,
required this.state,
});
final AuthState state;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
ElevatedButton(
onPressed: () {
state.logIn();
},
child: const Text('Log In'),
),
ElevatedButton(
onPressed: () {
context.go('/auth/signup');
},
child: const Text('Go to Sign Up'),
),
],
),
),
);
}
}
class MySignupPage extends StatelessWidget {
const MySignupPage({
super.key,
required this.state,
});
final AuthState state;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
ElevatedButton(
onPressed: () {
state.signUp();
},
child: const Text('Sign Up'),
),
ElevatedButton(
onPressed: () {
context.go('/auth/login');
},
child: const Text('Go to Log In'),
),
],
),
),
);
}
}
from flutter.
@daviddomkar
Glad it works for you, Good luck
from flutter.
Related Issues (20)
- [tool_crash] ShaderCompilerException HOT 3
- number keyboard doesn't work in TextField after update flutter to 3.22 HOT 2
- [Linux] FocusableActionDetector not working HOT 24
- [Web] Canvaskit raise errors on latest Chrome stable version HOT 24
- Adapt flutter tools to handle workspaces:
- Crash on linux HOT 6
- RangeError: RangeError: Value not in range: 226 HOT 4
- mac-10 & mac-26 lost external connection phone device. HOT 1
- mac-24 lost external connection phone device. HOT 2
- IOS Rendering Issue with Impeller on Version 3.22.2 HOT 3
- Skip Xcode automation handling with Xcode 16 and new deployment process
- Add support for pre-publish hooks for publishing first party Flutter packages HOT 1
- [android][platform view]investigate whether android has similar performance issue due to 1 px overlay HOT 3
- [Android] Build the engine against Android 35
- [Android] Maintain current (Android < 35) `SystemUiMode` behavior on Android >= 35 HOT 2
- Flutter supports Sensitive Content Protection HOT 2
- Image picker tests `testPickingFromGallery`, `testPickingWithContraintsFromGallery`, and `testSelectingFromGallery` failed on iOS 18 beta 1 HOT 2
- [tool_crash] _TypeError: (#0 MaterialFonts.updateInner (package:flutter_tools/src/flutter_cache.dart:151:49)) HOT 2
- LicensePage and LicenseRegistry.licenses have license from dev_dependencies in release HOT 1
- NSBundle: principal class is nil because all fallbacks have failed (Add-to-app, IOS 18, XCode 16 Beta 1) HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flutter.