Would it be possible with the new getPages API to dynamically set the home Page () ?
This would be great, that there is a default way to determine for example if a user has logged in or not to show Signup or Home page.
Now (in other issue) sometimes a Splash page is used in between to determine next page, but if you could dynamically return a Page(widget) as the root/home / page that would be great!
This can be done fairly easily as is with GetMaterialApp, initial binding, auth controller, initial route, and named routes. Below is a basic example using Firebase Auth.
In main.dart:
GetMaterialApp(
initialBinding: MainBinding(),
initialRoute: "/splash",
namedRoutes: {
"/splash": GetRoute(page: SplashView()),
"/login: GetRoute(page: LoginView(), binding: LoginBinding()),
"/home": GetRoute(page: HomeView(), binding: HomeBinding()),
},
)
In bindings/main.dart:
class MainBinding implements Bindings {
@override
void dependencies() {
Get.put<AuthController>(AuthController());
}
}
In controllers/auth.dart:
class AuthController extends RxController {
static AuthController get instance => Get.find();
Rx<FirebaseUser> currentUser;
@override
void onInit() async {
super.onInit();
currentUser = Rx(await auth.currentUser());
auth.onAuthStateChanged.listen((event) async {
if (event == null) {
currentUser.value = null;
Get.offAllNamed("/login");
} else {
currentUser.value = await auth.currentUser();
Get.offAllNamed("/home");
}
});
}
}
Yes this is how I do it today. The namedRoutes are deprecated now and through getPages you now have a function to return a Page. Did already some source code research and I think this will be a lot of easier from now on.
The only "issue" that I see is that
GetPage(
name: '/',
page: () { }
cannot be used with async.
Would it be possible with the new
getPagesAPI to dynamically set the home Page () ?
This would be great, that there is a default way to determine for example if a user has logged in or not to show Signup or Home page.
Now (in other issue) sometimes a Splash page is used in between to determine next page, but if you could dynamically return a Page(widget) as the root/home / page that would be great!
The first page will be called in the order of the list, in the next small update, if initialRoute is empty.
class AuthController extends RxController {
static AuthController get instance => Get.find();Rx
currentUser; @override
void onInit() async {
super.onInit();
currentUser = Rx(await auth.currentUser());
auth.onAuthStateChanged.listen((event) async {
if (event == null) {
currentUser.value = null;
Get.offAllNamed("/login");
} else {
currentUser.value = await auth.currentUser();
Get.offAllNamed("/home");
}
});
}
}
But I still like the idea of having a controller for auth, as in this example, it makes things a lot simpler when you have projects where it is necessary to disconnect users due to inactivity, or to ban users from the platform for breaching the terms, imperatively by server.
AuthController.hasSession = false; it is very practical.
Ok my only "problem" with it, is that you need another / root page first (like splash). I would like to have the /login or /home as the root page directly. Would that be possible?
Ok my only "problem" with it, is that you need another / root page first (like splash). I would like to have the /login or /home as the root page directly. Would that be possible?
Yes, as long as you have the data in hand.
As long as you have a given sync, you can simply do:
GetPage(
name: '/',
page: ()=> Auth.to.hasSession ? Home(): Login(),
This is one of the reasons why I want to integrate a simple key / value storage (json), using the Flutter path api, to make data reading synchronous. This would look something like this:
Get.write ("token", '8493804938409843');
// I would write the token asynchronously in json, and write the data directly into memory, to access it synchronously.
var token = Get.read("token");
bool hasToken = Get.hasKey("token");
GetPage(
name: '/',
page: ()=> Auth.to.hasSession ? Home(): Login(),
Imagine how easy it would be to do this:
GetPage(
name: '/',
page: ()=> Get.hasKey("token") ? Home(): Login(),
with namedRoutes when accessing the app using the url host/#/foo/bar the navigation stack would be:
HomeScreen
FooScreen
BarScreen
Now with getPages there's only the HomeScreen in the stack, has this functionality been deprecated too?
with namedRoutes when accessing the app using the url
host/#/foo/barthe navigation stack would be:HomeScreen
FooScreen
BarScreenNow with getPages there's only the HomeScreen in the stack, has this functionality been deprecated too?
At least as a rule he should go directly to BarScreen.
At least until version 2.0 of the Navigator is released it will probably be like that.
with namedRoutes when accessing the app using the url
host/#/foo/barthe navigation stack would be:HomeScreen
FooScreen
BarScreenNow with getPages there's only the HomeScreen in the stack, has this functionality been deprecated too?
At least as a rule he should go directly to BarScreen.
At least until version 2.0 of the Navigator is released it will probably be like that.
I will stick to namedRoutes while Navigator 2.0 isn't released then, our business logic require this functionality to work properly.
Imagine how easy it would be to do this:
GetPage( name: '/', page: ()=> Get.hasKey("token") ? Home(): Login(),
Would love this!
Added in Beta 3.0 in conjunction with GetStorage (I haven't decided yet if I create a "full" package and leave this one as core, or create a "core" and leave this one as full), but add version 2.0 together with get_storage should work.
Already using it with GetStorage now in current version!
how can I access beta? i have no idea of how to do that. I checked branches and did not found it, I checked that link in pub.dev and didn't found it. Where do i find that beta?
Most helpful comment
Yes, as long as you have the data in hand.
As long as you have a given sync, you can simply do:
This is one of the reasons why I want to integrate a simple key / value storage (json), using the Flutter path api, to make data reading synchronous. This would look something like this:
Get.write ("token", '8493804938409843');// I would write the token asynchronously in json, and write the data directly into memory, to access it synchronously.
var token = Get.read("token");bool hasToken = Get.hasKey("token");Imagine how easy it would be to do this: