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

Flutter Supabase查询报错:Map<String,dynamic>无data/error属性

修复Supabase Flutter查询中response.data/response.error的报错问题

问题原因

你使用的是Supabase Dart SDK v2.x版本,这个版本的API和旧版(v1.x)有重大变更:

  • v1.x中查询会返回包含data和error属性的Response对象
  • v2.x中,.single()这类查询方法成功时直接返回Map<String, dynamic>类型的结果,失败则直接抛出PostgrestException异常,不再有response.data或response.error属性,这就是报错的核心原因。

修复方案

改用try-catch块捕获异常,直接使用返回的Map数据:

void _fetchUserData() async {
  final user = Supabase.instance.client.auth.currentUser;
  if (user != null) {
    setState(() {
      _userId = user.id;
    });

    try {
      // 成功时直接拿到用户profile的Map数据
      final userProfile = await Supabase.instance.client
          .from('user_profiles')
          .select()
          .eq('id', user.id)
          .single();

      setState(() {
        _dobController.text = userProfile['date_of_birth'];
        _genderController.text = userProfile['gender'];
        _idController.text = userProfile['id_number'];
      });
    } on PostgrestException catch (e) {
      // 处理Supabase查询异常
      _showSnackBar(e.message);
    } catch (e) {
      // 兜底处理其他未知错误
      _showSnackBar('获取用户数据失败:$e');
    }
  }
}

补充说明

  • 如果你坚持使用v1.x版本,需要确保类型声明正确(比如final Response response = await ...),但官方已停止对v1.x的维护,建议升级到最新v2.x版本。
  • v2.x的API设计更简洁,所有查询异常都通过try-catch处理,避免了繁琐的response.error判断。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 12:15:13