Flutter中如何向Isar数据库添加嵌套字符串列表
解决Isar数据库嵌套字符串列表的"Unsupported type"错误
Isar本身不直接支持List<List<String>>这类嵌套列表类型,你需要用@Embedded类来封装内层的字符串列表,之前的错误大概率是没正确用Embedded类做包装。
正确实现步骤:
- 先定义一个带
@Embedded注解的类,用来承载内层的字符串列表:
import 'package:isar/isar.dart'; part 'user.g.dart'; @Embedded() class HistoryEntry { List<String>? items; }
- 在你的
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
相关产品推荐
相关产品推荐

