Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何抓住谷歌签名中的错误?_Flutter_Dart - Fatal编程技术网

Flutter 如何抓住谷歌签名中的错误?

Flutter 如何抓住谷歌签名中的错误?,flutter,dart,Flutter,Dart,我使用以下代码以静默方式登录用户 try { result = await GoogleSignIn().signInSilently().catchError((x) { print(x); }); } catch (e) { print(e); } 如果用户无法以静默方式登录,则会导致错误 PlatformException (PlatformException(sign_in_required, com.google.android.gms.c

我使用以下代码以静默方式登录用户

try {
    result = await GoogleSignIn().signInSilently().catchError((x) {
      print(x);
    });
  } catch (e) {
    print(e);
  }
如果用户无法以静默方式登录,则会导致错误

PlatformException (PlatformException(sign_in_required, com.google.android.gms.common.api.ApiException: 4: 4: , null))
我遇到的问题是,我似乎无法捕捉到异常。catchError或catch块被击中。如何捕获此错误?

可能会有所帮助

检查表:

  • 没有注册sha指纹
  • 确保设置了我的“支持电子邮件”
  • 启用Google登录方法

  • 在您的方法中执行以下操作

    try {
        result = await GoogleSignIn().signInSilently(suppressErrors: false).catchError((x) {
          print(x);
        });
      } catch (e) {
        print(e);
      }
    
    默认情况下
    suppressErrors=true
    抑制要捕获的错误消息

    看着

    显式方法用于抑制错误消息,因此不会引发要捕获的异常

    从该方法的文档中:

      /// When [suppressErrors] is set to `false` and an error occurred during sign in
      /// returned Future completes with [PlatformException] whose `code` can be
      /// either [kSignInRequiredError] (when there is no authenticated user) or
      /// [kSignInFailedError] (when an unknown error occurred).
    
    完整方法

     /// Attempts to sign in a previously authenticated user without interaction.
      ///
      /// Returned Future resolves to an instance of [GoogleSignInAccount] for a
      /// successful sign in or `null` if there is no previously authenticated user.
      /// Use [signIn] method to trigger interactive sign in process.
      ///
      /// Authentication process is triggered only if there is no currently signed in
      /// user (that is when `currentUser == null`), otherwise this method returns
      /// a Future which resolves to the same user instance.
      ///
      /// Re-authentication can be triggered only after [signOut] or [disconnect].
      ///
      /// When [suppressErrors] is set to `false` and an error occurred during sign in
      /// returned Future completes with [PlatformException] whose `code` can be
      /// either [kSignInRequiredError] (when there is no authenticated user) or
      /// [kSignInFailedError] (when an unknown error occurred).
      Future<GoogleSignInAccount> signInSilently({bool suppressErrors = true}) {
        final Future<GoogleSignInAccount> result = _addMethodCall('signInSilently');
        if (suppressErrors) {
          return result.catchError((dynamic _) => null);
        }
        return result;
      }
    
    ///尝试在没有交互的情况下登录以前经过身份验证的用户。
    ///
    ///返回的Future解析为一个
    ///成功登录或“null”(如果以前没有经过身份验证的用户)。
    ///使用[signIn]方法触发交互式登录过程。
    ///
    ///仅当当前没有登录时才会触发身份验证过程
    ///user(即当`currentUser==null`),否则此方法返回
    ///解析为同一用户实例的未来。
    ///
    ///只有在[signOut]或[disconnect]之后才能触发重新身份验证。
    ///
    ///当[suppressErrors]设置为'false'并且在登录期间发生错误时
    ///返回的Future以[PlatformException]完成,其'code'可以是
    ///[kSignInRequiredError](当没有经过身份验证的用户时)或
    ///[kSignInFailedError](发生未知错误时)。
    未来显著({bool suppressErrors=true}){
    最终未来结果=_addMethodCall('signitily');
    如果(抑制错误){
    返回result.catchError((dynamic )=>null);
    }
    返回结果;
    }
    
    参考文献


    如果使用
    suppressErrors=false

    试试下面的代码,看看它在控制台上打印了什么

    result = await GoogleSignIn().signInSilently(suppressErrors: false).
      catchError((x) {
        print("inside catchError");
      });
    

    问题是
    打印(e)catch语句中的行。'e'不是字符串,因此发生错误。我不知道为什么,但是在catch语句中没有为这个错误命中断点,也没有错误输出到控制台。如果我在调用登录代码的函数周围放一个catch语句

    void tryLogin(){
      try{
        myLoginLogic();
      } catch(e) {
        print(e);
      }
    }
    
    然后我确实收到了一条错误消息

    Unhandled Exception: type '_TypeError' is not a subtype of type 'String'
    
    因此,为了清楚起见,正确的代码应该是

    try {
      result = await GoogleSignIn().signInSilently().catchError((e) {
        print(e.toString());
      });
    } catch (e) {
      print(e.toString());
    }
    

    我不知道为什么代码没有在出错时中断,并且没有为原始函数向控制台写入消息。另外,我不明白的是,即使catch语句编写时没有错误,为什么在调试模式下,
    行的代码会明显中断。

    您好,谢谢您的输入。问题是捕获代码从未被命中。嗨,谢谢你的输入。问题是错误被抛出,只是没有被捕获,然后我包装代码的函数返回一个_TypeError类型,并且没有在catch块中运行代码。谢谢,这在我删除print(e)行的过程中帮助了我!