You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter集成Supabase Auth UI实现Google OAuth时onSuccess不执行问题

问题解决:Supabase Auth UI Google OAuth登录成功但onSuccess不执行

问题现象

  • Google OAuth登录已成功(Supabase控制台可确认账号登录状态),但SupaSocialsAuth的onSuccess回调函数未触发,无法执行页面跳转代码
  • 设备日志出现E/OpenGLRenderer( 8967): Unable to match the desired swap behavior.警告

环境信息

  • 设备:Pixel 5a
  • 使用包:supabase_auth_ui

解决方案

1. 忽略OpenGL警告

E/OpenGLRenderer的日志是Android系统层面的OpenGL渲染警告,不影响业务功能,无需处理。

2. 验证Redirect URL配置

确保以下两处配置完全一致:

  • Supabase控制台:在Auth -> URL配置中添加io.supabase.flutterquickstart://login-callback
  • AndroidManifest.xml:添加对应的intent-filter:
<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="io.supabase.flutterquickstart" />
</intent-filter>

3. 改用Auth状态监听替代onSuccess回调

部分场景下SupaSocialsAuth的onSuccess可能因回调流程问题不触发,手动监听Supabase的Auth状态变化更可靠:

修改原StatelessWidget为StatefulWidget,添加状态监听:

class SociallAuth extends StatefulWidget {
  @override
  State<SociallAuth> createState() => _SociallAuthState();
}

class _SociallAuthState extends State<SociallAuth> {
  late StreamSubscription<AuthState> _authSubscription;

  @override
  void initState() {
    super.initState();
    // 监听Auth状态变化
    _authSubscription = Supabase.instance.client.auth.onAuthStateChange.listen((data) {
      final event = data.event;
      final session = data.session;
      // 检测到登录成功事件且存在会话时执行跳转
      if (event == AuthChangeEvent.signedIn && session != null) {
        Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => const Home()),
        );
      }
    });
  }

  @override
  void dispose() {
    // 页面销毁时取消监听
    _authSubscription.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SupaSocialsAuth(
      colored: true,
      socialProviders: const [
        SocialProviders.google,
      ],
      redirectUrl: "io.supabase.flutterquickstart://login-callback",
      onError: (error) {
        debugPrint(error.toString());
      },
    );
  }
}

4. 确认Supabase初始化配置

确保main函数中Supabase初始化时正确配置深层链接:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Supabase.initialize(
    url: '你的Supabase项目URL',
    anonKey: '你的Supabase匿名密钥',
    deepLinkConfig: const DeepLinkConfig(
      androidPackageName: '你的Android包名',
      iosBundleId: '你的iOS Bundle ID',
    ),
  );
  runApp(const MyApp());
}

内容的提问来源于stack exchange,提问作者dev000

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 23:50:17