Skip to content

Instantly share code, notes, and snippets.

@stefandevo
Last active June 16, 2020 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefandevo/3e14e38c449da182d0b79e4551414b49 to your computer and use it in GitHub Desktop.
Save stefandevo/3e14e38c449da182d0b79e4551414b49 to your computer and use it in GitHub Desktop.
demo2.dart
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(DemoApp());
}
class DemoApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'Demo',
initialBinding: InitialBinding(),
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Splash(),
);
}
}
class InitialBinding extends Bindings {
@override
void dependencies() async {
final prefs = await SharedPreferences.getInstance();
Get.put<SharedPreferences>(prefs);
Get.put<ProfileController>(ProfileController());
}
}
class Splash extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold();
}
}
class ProfileController extends RxController {
static const profileKey = 'profileId';
var isLoggedIn = false;
var user = '';
static ProfileController get to => Get.find();
@override
void onInit() {
final prefs = Get.find<SharedPreferences>();
user = prefs.getString(profileKey);
isLoggedIn = (user != null);
}
@override
void onReady() {
checkLoggedInStatus();
super.onReady();
}
void checkLoggedInStatus() {
if (isLoggedIn) {
Get.off(HomePage());
} else {
Get.off(SignupPage(), binding: SignupBinding());
}
}
void signup() {
final prefs = Get.find<SharedPreferences>();
user = 'John Doe';
prefs.setString(profileKey, user);
isLoggedIn = true;
checkLoggedInStatus();
}
void logout() {
final prefs = Get.find<SharedPreferences>();
prefs.remove(profileKey);
isLoggedIn = false;
checkLoggedInStatus();
}
}
class SignupBinding extends Bindings {
@override
void dependencies() {
Get.put<SignupController>(SignupController());
}
}
class SignupController extends RxController {
void signup() {
final profileController = Get.find<ProfileController>();
profileController.signup();
}
}
class SignupPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final controller = Get.find<SignupController>();
return Scaffold(
appBar: AppBar(
title: Text('Signup'),
),
body: Center(
child: Column(
children: <Widget>[
RaisedButton(
child: Text("Do Signup"),
onPressed: () => controller.signup(),
)
],
),
),
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: Center(
child: Column(
children: <Widget>[
Container(
child: Text('Welcome ' + ProfileController.to.user),
),
RaisedButton(
child: Text("Logout"),
onPressed: () => ProfileController.to.logout(),
)
],
),
),
);
}
}
@stefandevo
Copy link
Author

Only difference with first demo is

  void checkLoggedInStatus() {
    if (isLoggedIn) {
      Get.off(HomePage());
    } else {
      Get.off(SignupPage(), binding: SignupBinding());
    }
  }

This would force to remove all underlying pages and make that the main page. So Splash -> SignupPage (should remove Splash) -> HomePage (should remove SignupPage).

But this fails:

flutter: [GET] GetMaterialController has been initialized
flutter: [GOING TO ROUTE] /
flutter: [GET] ProfileController has been initialized
flutter: [GET] SignupController has been initialized
flutter: [REPLACE ROUTE] /
flutter: [NEW ROUTE] /signuppage
flutter: [GET] SharedPreferences deleted from memory
flutter: [GET] onClose of ProfileController called
flutter: [GET] ProfileController deleted from memory
flutter: [GET] onClose of SignupController called
flutter: [GET] SignupController deleted from memory

════════ Exception caught by widgets library ═══════════════════════════════════
The following message was thrown building SignupPage(dirty):
 SignupController not found. You need call put<SignupController>(SignupController()) before

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment