I have a problem with my flutter app. When I run and use the login feature, it display: PlatformException (PlatformException(channel-error, Unable to establish connection on channel., null, null)). The flow: Sign in Button -> login_controller -> AuthenticationRepository.
Click here for the error picture
/// Sign in button
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: () => controller.emailAndPasswordSignIn(),
child: const Text(TTexts.signIn),
),
),
It will use the logic here, and the problem is at the AuthenticationRepository
class as shown in the picture (click on the hyperlink). I also have some custom class but I don’t think it will be helpful here. The comment inside the code I think it will be enough. I also have try the flutter clean and flutter pub get but still not working. All the package is up-to-date.
/// -- Email and Password SignIn
Future<void> emailAndPasswordSignIn() async {
try {
// Start Loading
TFullScreenLoader.openLoadingDialog(
'Logging you in...', TImages.docerAnimation);
// Check Internet Connectivity
final isConnected = await NetworkManager.instance.isConnected();
if (!isConnected) {
TFullScreenLoader.stopLoading();
return;
}
// Form Validation
if (!loginFormKey.currentState!.validate()) {
TFullScreenLoader.stopLoading();
return;
}
// Save Data if Remember Me is Selected
if (rememberMe.value) {
localStorage.write('REMEMBER_ME_EMAIL', email.text.trim());
localStorage.write('REMEMBER_ME_PASSWORD', password.text.trim());
}
// Login user using Email & Password Authentication
final userCredentials = await AuthenticationRepository.instance
.loginWithEmailAndPassword(email.text.trim(), password.text.trim());
// Save user record
await userController.saveUserRecord(userCredentials);
// Remove Loader
TFullScreenLoader.stopLoading();
// Redirect
AuthenticationRepository.instance.screenRedirect();
} catch (e) {
TFullScreenLoader.stopLoading();
TLoaders.errorSnackBar(title: 'Oh Snap!', message: e.toString());
}
}
class AuthenticationRepository extends GetxController {
static AuthenticationRepository get instance => Get.find();
/// Variables
final deviceStorage = GetStorage();
final _auth = FirebaseAuth.instance;
/// Get Authenticated User Data
User? get authUser => _auth.currentUser;
/// Called from main.dart on app laucnh
@override
void onReady() {
FlutterNativeSplash.remove();
screenRedirect();
}
/// Function to Show Relevant Screen
void screenRedirect() async {
final user = _auth.currentUser;
if (user != null) {
if (user.emailVerified) {
Get.offAll(() => const NavigationMenu());
} else {
Get.offAll(() => VerifyEmailScreen(
email: _auth.currentUser?.email,
));
}
} else {
// Local Storage
deviceStorage.writeIfNull('isFirstTime', true);
// Check if it's the first time launching the app
deviceStorage.read('isFirstTime') != true
? Get.offAll(() => const LoginScreen())
: Get.offAll(const OnBoardingScreen());
}
}
/* ---------- Email & Password sign-in -----------------*/
/// [EmailAuthenctication] - SignIn
Future<UserCredential> loginWithEmailAndPassword(
String email, String password) async {
try {
return await _auth.signInWithEmailAndPassword(
email: email, password: password); // this where the exception error happen
} on FirebaseAuthException catch (e) {
throw TFirebaseAuthException(e.code).message;
} on FirebaseException catch (e) {
throw TFirebaseException(e.code).message;
} on FormatException catch (_) {
throw const TFormatException();
} on PlatformException catch (e) {
throw TPlatformException(e.code).message;
} catch (e) {
throw 'Something went wrong. Please try again';
}
}
}
This is my first time doing a complex flutter project and quite newbie on backend/logic programming. Any help/discussion is welcome
2
Answers
This line causing that error
You can assign it like this way.
make sure the loginFormKey has been assigned to a Form widget, I think you missed it and the error says currentState is null.