Home > Software engineering >  my flutter firebase app always have same error
my flutter firebase app always have same error

Time:04-20

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

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,
            )
          ],
        ),
      ),
    );
  }
}

CodePudding user response:

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.

  • Related