Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

my flutter firebase app always have same error

I have made a online store app with flutter and backend on firebase , in sign up page whenever click on create account button it says same error, no matter if I put data on Text Fields or not, I am really confused about this!

the error is

given String is empty or null

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../widgets/custom_button.dart';
import '../widgets/custom_input.dart';
import 'const.dart';
import 'login.dart';

class SingUP extends StatefulWidget {
  SingUP({Key? key}) : super(key: key);

  @override
  State<SingUP> createState() => _SingUPState();
}

class _SingUPState extends State<SingUP> {
  Future<void> advanceAlertDialog(String message) {
    return Get.defaultDialog(
      title: 'Invalid Input',
      content: Text(message),
      actions: [
        ElevatedButton(
          onPressed: () => Get.back(),
          child: Text('OK !'),
        )
      ],
      contentPadding: EdgeInsets.all(15.0),
    );
  }

  Future<String?> signInMethod() async {
    try {
      await FirebaseAuth.instance.createUserWithEmailAndPassword(
          email: takeEmail, password: takePassword);
      return 'ok';
    } on FirebaseAuthException catch (error) {
      if (error.code == 'weak-password') {
        return 'the password is too week';
      } else if (error.code == 'email-already-in-use') {
        return 'The account with this email is already exist !';
      }
      return error.message;
    } catch (error) {
      return error.toString();
    }
  }

  void signIn() async {
    setState(() {
      isLoading = true;
    });
    String? result = await signInMethod();
    if (result != 'ok') {
      advanceAlertDialog(result!);
      setState(() {
        isLoading = false;
      });
    } else {
      setState(() {
        isLoading = false;
      });
      Get.to(LoginPage());
      Get.snackbar('Successful', 'Account Created !',
          backgroundColor: Colors.greenAccent, icon: Icon(Icons.check_circle));
    }
  }

  bool isLoading = false;
  String takeEmail = '';
  String takePassword = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: double.infinity,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Container(
              padding: EdgeInsets.only(top: 50),
              child: Text(
                'Create a New Account !',
                style: Constans.headingTextStyle,
                textAlign: TextAlign.center,
              ),
            ),
            Column(
              children: [
                CustomInput(
                  hintText: 'Enter Your Email ...',
                  onChanged: (value) {
                    takeEmail = value;
                  },
                  checkPassword: false,
                ),
                CustomInput(
                  hintText: 'Enter Your Password ...',
                  onChanged: (value) {
                    takeEmail = value;
                  },
                  checkPassword: true,
                ),
                CustomButton(
                  text: 'Create Account',
                  loading: isLoading,
                  onTap: () {
                    signIn();
                  },
                  mode: false,
                )
              ],
            ),
            CustomButton(
              text: 'Back to Login',
              onTap: () => Get.back(),
              mode: true,
              loading: isLoading,
            )
          ],
        ),
      ),
    );
  }
}

>Solution :

Emain and password widgets are the same takeEmail = value;, change the password onChange value to takePassword = value;, also where is exactly the error thrown?. I advice to add breakpoints to signInMethod() to see the input values of email: takeEmail, password: takePassword (both of this strings) and after the on FirebaseAuthException catch (error) { to see if the error is thrown from firebase or is in your code.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading