I have been working on this Getx library for the first time and i don't know why but the initial screen function is not working for some reason. It's not showing any bug in the stack trace so i guess it's gotta be something else. has anyone any idea? Debug shows no error or warnings.
here's the code
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tutorial_two/screens/screen_home.dart';
import 'package:tutorial_two/screens/screen_login.dart';
import 'package:velocity_x/velocity_x.dart';
class AuthController extends GetxController {
static AuthController instance =
Get.find(); // auth controller instance; will be called in other widgets
// user instance
late Rx<User?> _user;
// get user data like name, email, password etc
FirebaseAuth auth = FirebaseAuth.instance; // firebase auth instance
@override
void onReady() {
super.onReady();
_user = Rx<User?>(auth.currentUser); // getting current user
_user.bindStream(
auth.userChanges(),
); //notifies app about user login and logout
ever(_user,
_initialScreen); //this funnction will make sure user gets to correct screen
}
_initialScreen(User? user) {
if (user == null) {
Get.offAll(() => const LoginScreen());
} else {
Get.offAll(() => const HomeScreen());
}
}
Future<void> register(
BuildContext context, String email, username, password) async {
try {
await auth.createUserWithEmailAndPassword(
email: email, password: password);
//await Future.delayed(const Duration(seconds: 2));
Future.delayed(
const Duration(
seconds: 1,
), () {
VxToast.show(context,
msg: "Registration Successful",
bgColor: Colors.green.shade100,
textColor: Colors.green.shade500,
textSize: 14,
position: VxToastPosition.center);
});
} catch (e) {
VxToast.show(context,
msg: "Error: $e",
bgColor: Colors.red.shade100,
textColor: Colors.red.shade500,
textSize: 14,
position: VxToastPosition.center);
}
}
Future<void> login(BuildContext context, String email, password) async {
try {
await auth.signInWithEmailAndPassword(email: email, password: password);
//await Future.delayed(const Duration(seconds: 2));
Future.delayed(
const Duration(
seconds: 1,
), () {
VxToast.show(context,
msg: "Login Successful",
bgColor: Colors.green.shade100,
textColor: Colors.green.shade500,
textSize: 14,
position: VxToastPosition.center);
});
} catch (e) {
VxToast.show(context,
msg: "Error: $e",
bgColor: Colors.red.shade100,
textColor: Colors.red.shade500,
textSize: 14,
position: VxToastPosition.center);
}
}
Future<void> logOut(BuildContext context) async {
try {
await auth.signOut();
} catch (e) {
VxToast.show(context,
msg: "Error: $e",
bgColor: Colors.red.shade100,
textColor: Colors.red.shade500,
textSize: 14,
position: VxToastPosition.center);
}
}
}
here's the main code
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tutorial_two/screens/screen_home.dart';
import 'package:tutorial_two/screens/screen_login.dart';
import 'package:tutorial_two/screens/screen_signup.dart';
import 'package:tutorial_two/screens/screen_splash.dart';
import 'package:tutorial_two/utils/auth_controller.dart';
import 'package:tutorial_two/utils/routes.dart';
import 'package:tutorial_two/widgets/themes.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
Firebase.initializeApp().then((value) {
Get.put(AuthController());
});
runApp(const TutorialTwo());
}
class TutorialTwo extends StatelessWidget {
const TutorialTwo({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
themeMode: ThemeMode.system,
theme: MyTheme.lightTheme(context),
darkTheme: MyTheme.darkTheme(context),
debugShowCheckedModeBanner: false,
home: const SplashScreen(),
routes: {
MyRoutes.homeRoute: (context) => const HomeScreen(),
MyRoutes.loginRoute: (context) => const LoginScreen(),
MyRoutes.signupRoute: (context) => const SignupScreen(),
},
);
}
}
CodePudding user response:
To use Get
navigation management, you must use GetMaterialApp
instead of MaterialApp
,
//Replace the following line
return MaterialApp(
//with this one
return GetMaterialApp(