简体   繁体   中英

Why am I having issues with my flutter firebase authentification?

My goal is to sign in with an existing account on my firebase data and show exceptions when the account is not existing... I followed some tutorials and the official firebase and flutter documentation. When I'm trying to log with an not existing or existing account the button login to naviguate in the home screen doesn't work.

Plus I have this error message type 'String' is not a subtype of type 'TextEditingController' in type cast

I have two pages code for this function

First my code in auth Provider Service Screen:

import 'package:firebase_auth/firebase_auth.dart';

class AuthClass {
  FirebaseAuth auth = FirebaseAuth.instance;

  Future<String?> signIN({required String email, required String password}) async {
    try {
      await auth.signInWithEmailAndPassword(
          email: email,
          password: password,);
      return "Welcome";
    } on FirebaseAuthException catch (e) {
      if (e.code == 'user-not-found') {
        return 'No user found for that email.';
      } else if (e.code == 'wrong-password') {
        return 'Wrong password provided for that user.';
      }
    }
  }
}

and this is my body login screen code:

import 'package:yona/WelcomePage/WelcomePage.dart';

class Body extends StatefulWidget {
  const Body({
    Key? key,
  }) : super(key: key);

  @override
  _BodyState createState() => _BodyState();
}

class _BodyState extends State<Body> {
  final GlobalKey<FormState> _formKey  = GlobalKey<FormState>();
  final auth = FirebaseAuth.instance;
  TextEditingController _email = TextEditingController();
  TextEditingController _password = TextEditingController();


  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery
        .of(context)
        .size;
    return Background(
        child: Form(
        key: _formKey,
        child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
        Text("LOGIN", style: TextStyle(
            color: Colors.white,
            fontWeight: FontWeight.bold,
            fontSize: 20)),
          Container(child: Image.asset('assets/ctlogo.png', height: 200),
          ),
       // Container
        RoundedInputField(
            hintText: "Your Email",
            onChanged: (value) {
              setState(() {
                _email = value.trim() as TextEditingController;
              });
            }
        ),
        RoundedPasswordField(
          onChanged: (value) {
            setState(() {
              _password = value.trim() as TextEditingController;
            });
          },
        ),
        Container(margin: EdgeInsets.symmetric(vertical: 20),
          width: 300,
          child: ClipRRect(
            borderRadius: BorderRadius.circular(29),
            // ignore: deprecated_member_use
            child: FlatButton(
              padding: EdgeInsets.symmetric(vertical: 14, horizontal: 40),
              color: SandYellow,
              onPressed: () {
                AuthClass()
                    .signIN(
                    email: _email.text.trim(), password: _password.text.trim())
                    .then((value) {
                  if (value == 'Welcome') {
                    Navigator.pushAndRemoveUntil(
                      context,
                      MaterialPageRoute(
                        builder: (context) => HomeScreen()),
                      (route) => false);
                    }
                  else{
                    ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(value!)));
                  }
                  }
                );
              },
              child: Text("LOGIN",
                  style: TextStyle(color: DarkTurquoise,
                    fontSize: 16,)
              ),
            ),
          ),
        ),
value.trim() as TextEditingController;

value is already String so you can not type cast to TextEditingController. _email variable should be a String in that way.

You can just delete as TextEditingController this part. so It will be worked.

Edit:

TextEditingController _email = TextEditingController();
TextEditingController _password = TextEditingController();

these variables should not be a Controller, should be a String.

String _email = '';
String _password = '';

No need to setState when changing TextEditingController value.

onChanged: (value) {
  _email.text = value.trim();
}),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM