skip to Main Content

This is a class UniqueId which is a valueobject. This is for checking equality and all that

class UniqueId extends ValueObject<String> {
      @override
      final Either<ValueFailure<String>, String> value;
    
      factory UniqueId() {
        return UniqueId._(
          Right(const Uuid().v1())
        );
      }
      factory UniqueId.fromUniqueString(String uniqueId) {
        return UniqueId._(Right(uniqueId));
      }
    
     const UniqueId._(
        this.value,
      );
    }

Then I have used freezed.

@freezed
abstract class User1 with _$User1{
  const factory User1(
    {required UniqueId id,}
  ) = _User1;
}

Now I am giving the part I am having problem with

@LazySingleton(as: IauthFailure)
class FirebaseAuthFailure implements IauthFailure {
  final FirebaseAuth firebaseAuth;
  final GoogleSignIn googleSignIn;

  FirebaseAuthFailure(this.firebaseAuth, this.googleSignIn); 
  @override
  Future<Option<User1>> getSignedInUser() =>
      firebaseAuth.currentUser().then((firebaseUser) => 
      optionOf(firebaseUser?.toDomain()));

The error I am getting is given below:
error1: The function can’t be unconditionally invoked because it can be ‘null’.
solution I used:

 @override
  Future<Option<User1>> getSignedInUser() =>
      firebaseAuth.currentUser!().then((firebaseUser) => optionOf(firebaseUser?.toDomain()));

error2:the expression doesn’t evaluate to a function, so it can’t be invoked.
solution I used:

 @override
  Future<Option<User1>> getSignedInUser() =>
      firebaseAuth.currentUser!.then((firebaseUser) => optionOf(firebaseUser?.toDomain()));

error3:The method ‘then’ isn’t defined for the type ‘User’.
Try correcting the name to the name of an existing method, or defining a method named ‘then’.
solution I used: Cannot find a solution
The definition of toDomain() is :

extension FirebaseUserDomainX on User {
  User1 toDomain() {
    return User1(id: UniqueId.fromUniqueString(uid));
  }
}

Actually I am not getting this toDomain when I am hovering over it and click go to definition its saying no defintion.

2

Answers


  1. If i understood it correctly (your question is not very clear), you should update your code accordingly to deprecated methods.
    currentUser is not a method anymore, but it is a getter.
    also I’m assuming _firebaseAuth is an instance of FirebaseAuth

    @override
    Future<Option<User>> getSignedInUser() async {
      User? firebaseUser = _firebaseAuth.currentUser;
      return optionOf(firebaseUser?.toDomain());
    }
    

    It would be very valuable if you posted the error that you’re getting when using optionOf

    Login or Signup to reply.
  2. I have found the error of it.

    @override
      Future<Option<User1>> getSignedInUser() =>
          firebaseAuth.currentUser!.then((firebaseUser) => optionOf(firebaseUser?.toDomain()));
    

    see this solution and try it.

    import 'package:dartz/dartz.dart';
    import 'package:demo_of_ddd/domain/auth/i_auth_facade.dart';
    import 'package:demo_of_ddd/domain/auth/user.dart';
    import 'package:demo_of_ddd/domain/core/value_objects.dart';
    import 'package:demo_of_ddd/infrastructure/auth/firebase_user_mapper.dart';
    import 'package:firebase_auth/firebase_auth.dart' hide User;
    
    import 'package:flutter/services.dart';
    import 'package:google_sign_in/google_sign_in.dart';
    import 'package:injectable/injectable.dart';
    
    import 'package:demo_of_ddd/domain/auth/auth_faild.dart';
    import 'package:demo_of_ddd/domain/auth/value_objects.dart';
    
    @named
    @Injectable(as: IAuthFacade)
    class FirebaseAuthFacade implements IAuthFacade {
      final FirebaseAuth _firebaseAuth;
      final GoogleSignIn _googleSignIn;
      FirebaseAuthFacade(
        this._firebaseAuth,
        this._googleSignIn,
      );
    
      @override
      Future<Option<User>> getSignedInUser() async {
        final firebaseUser = _firebaseAuth.currentUser;
    
        if (firebaseUser != null) {
          return optionOf(firebaseUser.toDomain());
        } else {
          return none();
        }
      }
    
      @override
      Future<Either<AuthFailure, Unit>> registerWithEmailAndPassword({
        required EmailAddress emailAddress,
        required Password password,
      }) async {
        // final user = _firebaseAuth.currentUser;
        // final userId = user != null ? user.uid : null;
    
        //
        final emailAddressStr = emailAddress.getOrCrash();
        final passwordStr = password.getOrCrash();
    
        try {
          await _firebaseAuth.createUserWithEmailAndPassword(
            email: emailAddressStr,
            password: passwordStr,
          );
          return right(unit);
        } on PlatformException catch (e) {
          if (e.code == 'ERROR_EMAIL_ALREADY_IN_USE') {
            return left(const AuthFailure.emailAlreadyInUse());
          } else {
            return left(const AuthFailure.serverError());
          }
        }
      }
    
      @override
      Future<Either<AuthFailure, Unit>> signInWithEmailAndPassword({
        required EmailAddress emailAddress,
        required Password password,
      }) async {
        final emailAddressStr = emailAddress.getOrCrash();
        final passwordStr = password.getOrCrash();
    
        try {
          await _firebaseAuth.signInWithEmailAndPassword(
            email: emailAddressStr,
            password: passwordStr,
          );
          return right(unit);
        } on PlatformException catch (e) {
          if (e.code == 'wrong-password' || e.code == 'user-not-found') {
            return left(const AuthFailure.invalidEmailAndPasswordCombination());
          } else {
            return left(const AuthFailure.serverError());
          }
        }
      }
    
      @override
      Future<Either<AuthFailure, Unit>> signInWithGoogle() async {
        try {
          final googleUser = await _googleSignIn.signIn();
          if (googleUser == null) {
            return left(const AuthFailure.cancelledByUser());
          }
          final googleAuthentication = await googleUser.authentication;
    
          final authCredential = GoogleAuthProvider.credential(
            idToken: googleAuthentication.idToken,
            accessToken: googleAuthentication.accessToken,
          );
    
          return _firebaseAuth
              .signInWithCredential(authCredential)
              .then((r) => right(unit));
        } on PlatformException catch (_) {
          return left(const AuthFailure.serverError());
        }
      }
    
      @override
      Future<void> signOut() async {
        await _googleSignIn.signOut();
        await _firebaseAuth.signOut();
      }
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search