I have a code which is correct in a 3 years back flutter but not in the current flutter version

huangapple go评论115阅读模式
英文:

I have a code which is correct in a 3 years back flutter but not in the current flutter version

问题

以下是您提供的代码片段的翻译:

这是一个名为UniqueId的类,它是一个值对象。用于检查相等性等等。

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

然后我使用了freezed。

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

现在我提供我遇到问题的部分。

@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()));

我遇到的错误如下:

错误1:无法无条件调用该函数,因为它可能为null。
我使用的解决方案:

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

错误2:该表达式不会评估为函数,因此无法调用。
我使用的解决方案:

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

错误3:该方法'then'对于类型'User'未定义。
我无法找到解决方案。

'toDomain()'的定义如下:

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

实际上,当我将鼠标悬停在上面并点击跳转到定义时,它显示没有定义。

英文:

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

class UniqueId extends ValueObject&lt;String&gt; {
      @override
      final Either&lt;ValueFailure&lt;String&gt;, String&gt; 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&lt;Option&lt;User1&gt;&gt; getSignedInUser() =&gt;
      firebaseAuth.currentUser().then((firebaseUser) =&gt; 
      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&lt;Option&lt;User1&gt;&gt; getSignedInUser() =&gt;
      firebaseAuth.currentUser!().then((firebaseUser) =&gt; optionOf(firebaseUser?.toDomain()));

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

 @override
  Future&lt;Option&lt;User1&gt;&gt; getSignedInUser() =&gt;
      firebaseAuth.currentUser!.then((firebaseUser) =&gt; 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.

答案1

得分: 0

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.

英文:

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&lt;Option&lt;User&gt;&gt; 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

答案2

得分: 0

I have found the solution of this error this code is deprecated. that's why is not working properly.

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

see this solution and try it. I have made a simple solution for the deprecated code it's run properly.

@override
Future<Option<User>> getSignedInUser() async {
  final firebaseUser = _firebaseAuth.currentUser;

  if (firebaseUser != null) {
    return optionOf(firebaseUser.toDomain());
  } else {
    return none();
  }
}
英文:

I have found the solution of this error this code is deprecated. that's why is not working properly.

@override
  Future&lt;Option&lt;User1&gt;&gt; getSignedInUser() =&gt;
      firebaseAuth.currentUser!.then((firebaseUser) =&gt; optionOf(firebaseUser?.toDomain()));


see this solution and try it. I have made a simple solution for the deprecated code it's run properly.

  @override
  Future&lt;Option&lt;User&gt;&gt; getSignedInUser() async {
    final firebaseUser = _firebaseAuth.currentUser;

    if (firebaseUser != null) {
      return optionOf(firebaseUser.toDomain());
    } else {
      return none();
    }
  }


huangapple
  • 本文由 发表于 2023年6月30日 03:03:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定