Flutter应用从浏览器重定向返回时遇PlatformException(null_intent)错误求助
问题详情
我使用带code参数的重定向URL com.example.crewdogcvlab://redirect 将浏览器请求导回Flutter应用,但返回时触发以下错误:
PlatformException(null_intent, Failed to authorize: Null intent received, null, null)
对应的报错日志:
E/flutter (22869): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(null_intent, Failed to authorize: Null intent received, null, null) E/flutter (22869): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:651:7) E/flutter (22869): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:322:18) E/flutter (22869): <asynchronous suspension> E/flutter (22869): #2 MethodChannelFlutterAppAuth.authorize (package:flutter_appauth_platform_interface/src/method_channel_flutter_appauth.dart:21:9) E/flutter (22869): <asynchronous suspension> E/flutter (22869): #3 loginAction (package:crewdogcvlab/screens/controllers/sso_authentication.dart:22:18) E/flutter (22869): <asynchronous suspension> E/flutter (22869): #4 _WelcomeScreenState.build.<anonymous closure> (package:crewdogcvlab/screens/welcome_screen.dart:85:29) E/flutter (22869): <asynchronous suspension>
排查与修复方案
校验AndroidManifest.xml的Intent-Filter配置
确保Android项目的AndroidManifest.xml中,处理重定向的Activity(通常是MainActivity)正确配置intent-filter,且包含必要的属性:<activity android:name=".MainActivity" android:exported="true"> <!-- 原有启动配置 --> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> <!-- 重定向URL处理配置 --> <intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.BROWSABLE"/> <data android:scheme="com.example.crewdogcvlab" android:host="redirect"/> </intent-filter> </activity>注意:Android 12+必须设置
android:exported="true",否则外部应用无法唤起该Activity。确认AppAuth初始化的重定向URI完全匹配
调用authorize方法时,传入的redirectUri必须和Manifest配置完全一致:final AuthorizationResponse? result = await appAuth.authorize( AuthorizationRequest( clientId, 'com.example.crewdogcvlab://redirect', // 与Manifest配置严格匹配 scopes: ['openid', 'profile'], issuer: issuerUri, ), );检查认证服务器的重定向URI白名单
确保你的OAuth2/OpenID认证服务器已将com.example.crewdogcvlab://redirect添加到合法重定向URI列表中,未注册的URI会导致服务器拒绝回调,进而引发空intent问题。测试重定向URL是否能唤起应用
在设备浏览器中直接输入com.example.crewdogcvlab://redirect?code=test,如果无法唤起应用,说明scheme配置存在问题,需重新检查Manifest的intent-filter。升级flutter_appauth依赖版本
部分旧版本的flutter_appauth存在intent处理bug,尝试升级到最新稳定版:
修改pubspec.yaml:dependencies: flutter_appauth: ^6.0.0 # 替换为最新版本执行
flutter pub get更新依赖。
内容的提问来源于stack exchange,提问作者Adnan Ashraf

