0

i am working on the creation/log for a user but i have multiple error, here is my code, and after my error : LoginController.dart :

  String? _mail;
  String? _password;
  String? _prenom;
  String? _nom;

  _handlelog(){
    if(_mail != null){
      if(_password != null){
        if(_log == true){
          //se connecter
          FirebaseHelper().handleSignIn(_mail, _password).then(((User user) {
            print("Nous avons un user");
          })).catchError((error){
            alerte(error.toString());
          });
        }else{
          if(_prenom != null){
            if(_nom != null){
              //créer un compte avec les données de l'utilisateur
              FirebaseHelper().handleCreate(_mail, _password, _prenom, _nom).then(((User user) {
                print("Nous avons pu créer un user");
              })).catchError((error){
                alerte(error.toString());
              });
            }else{
              //Alert nom
              alerte("veuillez entrer un nom pour continuer");
            }
          }else{
            //Alert prenom
            alerte("veuillez entrer un prenom pour continuer");
          }
        }
      }else{
        //Alert password
        alerte("le mot de passe est vide");
      }
    }else{
      //Alert mail
      alerte("l'adresse mail est vide");
    }
  }

FirebaseHelper.dart :

class FirebaseHelper {

  //Authentification
  final auth = FirebaseAuth.instance;

  Future<UserCredential> handleSignIn(String mail, String password) async{
    final UserCredential user = await auth.signInWithEmailAndPassword(email: mail, password: password);
    return user;
  }

  Future<UserCredential> handleCreate(String mail, String password, String prenom, String nom) async{
    final UserCredential credential = await FirebaseAuth.instance.createUserWithEmailAndPassword(email: mail, password: password);
    String uid = credential.user!.uid;
    Map<String, String> map = {
      "uid" : uid,
      "prenom" : prenom,
      "nom" : nom,
    };
    addUser(uid, map);
    return credential;
  }

  //database

  static final base = FirebaseDatabase.instance.reference();
  // ignore: non_constant_identifier_names
  final base_user = base.child("users");

  addUser(String uid, Map map){
    base_user.child(uid).set(map);
  }

}

In LoginController.dart i have The argument type 'String?' can't be assigned to the parameter type 'String'. The argument type 'Null Function(User)' can't be assigned to the parameter type 'FutureOr<dynamic> Function(UserCredential)'. I don't know what i can do, i search very long time on docs, on this website for found more detail, but i found nothing, i'm despair

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
et1000_
  • 99
  • 1
  • 3
  • 6

2 Answers2

0

I have used this, maybe it will help

class AuthServices {
final FirebaseAuth _auth = FirebaseAuth.instance;
  //Create user object based on firebase user
  UserData _userFromFirebaseUser(User user) {
    return user != null ? UserData(uid: user.uid, email: user.email) : null;
  }

  //Auth change user stream
  Stream<UserData> get uservalue {
    return _auth.authStateChanges().map((User uservalue) => _userFromFirebaseUser(uservalue));
  }


  // Sign In with email & password
  Future sigInUser(String email, String password) async {
    try {
      UserCredential result = await _auth.signInWithEmailAndPassword(email: email, password: password);
      User userval = result.user;

      return _userFromFirebaseUser(userval);

    }

    catch(e) {
      print(e.toString());
      return null;
    }
  }

  // Register with email & password
  Future registerUser(String email, String password) async {
    try {
      UserCredential result = await _auth.createUserWithEmailAndPassword(email: email, password: password);
      User userval = result.user;

      return _userFromFirebaseUser(userval);

    }
    catch(e) {
      print(e.toString());
    }

  }
}
sungkd123
  • 383
  • 1
  • 8
0

Error 1:

The argument type 'String?' can't be assigned to the parameter type 'String'.

The error above is showing because _mail, _password, _prenom and _nom are of type String? which means they can be null while the arguments for the FirebaseHelper().handleCreate() method are of type String which is not the same as String?.

A postfix exclamation mark (!) takes the expression on the left and casts it to its underlying non-nullable type

https://dart.dev/null-safety/understanding-null-safety#null-assertion-operator

So adding ! casts the String? objects to type String.

Error 2:

The argument type 'Null Function(User)' can't be assigned to the parameter type 'FutureOr Function(UserCredential)'.

The reason for the error above is that FirebaseHelper().handleCreate() returns a type of Future<UserCredential> but you're using User as the type in your LoginController.dart file.

Solution:

You need to update this part of your code:

FirebaseHelper().handleCreate(_mail, _password, _prenom, _nom).then(((User user) {

to this:

FirebaseHelper().handleCreate(_mail!, _password!, _prenom!, _nom!).then(((UserCredential user) {
Victor Eronmosele
  • 7,040
  • 2
  • 10
  • 33
  • i have an error, when i want to create or log in an account (on my application), it's mark : `[firebase_auth/internal-error]{"error":{"code":400,"message":"CONFIGURATION_NOT_FOUND","errors":[{"message":"CONFIGURATION_NOT_FOUND","domain":"global","reason":"invalid"}]}}` if you want all my code, so i can post a link to it : [link](https://ayvemgroup.000webhostapp.com/txt.txt) – et1000_ Jun 11 '21 at 06:39
  • Have you registered your app/ the authentication service you're using on Firebase? Check out this link: https://stackoverflow.com/a/65453612/11039164. – Victor Eronmosele Jun 11 '21 at 08:56
  • Also, does my answer solve your initial question? – Victor Eronmosele Jun 11 '21 at 08:56