import 'dart:convert'; import 'package:objectbox/objectbox.dart'; @Entity() class Ranking { @Id() int id = 0; // Store ranks as JSON string String ranksJson = '[]'; @Transient() List get ranks { final List decoded = jsonDecode(ranksJson); return decoded.map((e) => Rank.fromJson(e as Map)).toList(); } set ranks(List value) { ranksJson = jsonEncode(value.map((e) => e.toJson()).toList()); } Ranking(); factory Ranking.fromJson(Map json) { final ranking = Ranking(); ranking.id = json['id'] as int? ?? 0; ranking.ranks = (json['ranking'] as List) .map((e) => Rank.fromJson(e as Map)) .toList(); return ranking; } Map toJson() { return { 'id': id, 'ranking': ranks.map((e) => e.toJson()).toList(), }; } } class Rank { late int id; late String zip; String? area; late List vehicles; Rank(); factory Rank.fromJson(Map json) { final rank = Rank(); rank.id = json['id'] as int; rank.zip = json['zip'] as String; rank.area = json['area'] as String?; rank.vehicles = (json['vehicles'] as List) .map((e) => Vehicle.fromJson(e as Map)) .toList(); return rank; } Map toJson() { return { 'id': id, 'zip': zip, 'area': area, 'vehicles': vehicles.map((e) => e.toJson()).toList(), }; } } class Vehicle { late int id; late int type; int? cnt; bool? self; Vehicle(); factory Vehicle.fromJson(Map json) { final vehicle = Vehicle(); vehicle.id = json['id'] as int; vehicle.type = json['type'] as int; vehicle.cnt = json['cnt'] as int?; vehicle.self = json['self'] as bool? ?? false; return vehicle; } Map toJson() { return { 'id': id, 'type': type, 'cnt': cnt, 'self': self, }; } }