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

Flutter中如何向Isar数据库添加嵌套字符串列表

解决Isar数据库嵌套字符串列表的"Unsupported type"错误

Isar本身不直接支持List<List<String>>这类嵌套列表类型,你需要用@Embedded类来封装内层的字符串列表,之前的错误大概率是没正确用Embedded类做包装。

正确实现步骤:

  1. 先定义一个带@Embedded注解的类,用来承载内层的字符串列表:
import 'package:isar/isar.dart';

part 'user.g.dart';

@Embedded()
class HistoryEntry {
  List<String>? items;
}
  1. 在你的User集合类中,用这个嵌入式类的列表替代原来的嵌套列表:
@Collection()
class User {
  Id? id;
  late String name;
  late String email;
  List<HistoryEntry>? history; // 替换原List<List<String>>类型
  @Index()
  late String phone;
}

原理说明:

Isar仅支持存储一级列表,嵌套列表需要通过嵌入式类做一层封装。完成上述修改后,执行代码生成命令flutter pub run build_runner build,就能生成符合要求的数据库schema了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 14:24:51