1

我正在尝试在配置单元数据库中缓存 API 数据。我已经从 api 获取数据,然后尝试保存在数据库中。它已成功保存,但在获取数据时出现错误。我猜它没有阅读子列表。这是数据类的代码

List<Character> characterFromJson(String str) =>
List<Character>.from(json.decode(str).map((x) => Character.fromJson(x)));

String characterToJson(List<Character> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

@HiveType(typeId: 0)
class Character {
@HiveField(0)
String url;
@HiveField(1)
String name;
@HiveField(2)
String gender;
@HiveField(3)
String culture;
@HiveField(4)
String born;
@HiveField(5)
String died;
@HiveField(6)
List<String> titles;
@HiveField(7)
List<String> aliases;
@HiveField(8)
String father;
@HiveField(9)
String mother;
@HiveField(10)
String spouse;
@HiveField(11)
List<String> allegiances;
@HiveField(12)
List<String> books;
@HiveField(13)
List<String> povBooks;
@HiveField(14)
List<String> tvSeries;
@HiveField(15)
List<String> playedBy;

Character({
this.url,
@required this.name,
this.gender,
this.culture,
this.born,
this.died,
this.titles,
this.aliases,
this.father,
this.mother,
this.spouse,
this.allegiances,
this.books,
this.povBooks,
this.tvSeries,
this.playedBy,
});

 

从 db 获取数据的代码:

List<Character> get storedCharacters {
  final box = Hive.box('data');
  var characters = box.toMap().values.toList();
  return characters;
} 
4

1 回答 1

3

.fromMap()假设您在 Characters 类中有一个方法,请将其更改为此。

List<Character> get storedCharacters {
  final box = Hive.box('data');
  List<Character> characters = [];
  for(var item in box.toMap().values){
    characters.add(Character.fromMap(item));
  }
   return characters;
 } 
于 2021-04-29T22:09:54.387 回答