gpt4 book ai didi

flutter - HiveError (HiveError : Cannot write, unknown type : Slug. Did you forget to register an adapter?)

转载 作者:行者123 更新时间:2023-12-05 06:11:13 27 4
gpt4 key购买 nike

我正在构建一个非常简单的应用程序来流式传输来自热门动漫网站的动漫,但是我在使用 Hive 时遇到了困难。

我正在尝试从动漫网站获取所有动漫,然后将其保存到 Hive 中的缓存中。但是我遇到了问题,这阻碍了我的发展。我注册了一个适配器,但 Hive 告诉我我没有。

有没有人知道如何修复它?

我的代码

lib/main.dart

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:twist_go/pages/nav_screen.dart';

void main() async {
runApp(EasyLocalization(
supportedLocales: [Locale('en', 'US')],
path: 'lib/assets/translations',
fallbackLocale: Locale('en', 'US'),
child: TwistGO(),
));
}

class TwistGO extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primaryColor: Colors.blueGrey[800],
scaffoldBackgroundColor: Colors.blueGrey[700],
),
localizationsDelegates: context.localizationDelegates,
supportedLocales: context.supportedLocales,
locale: context.locale,
home: NavScreen(),
);
}
}

模型/anime_all.dart

import 'dart:convert';

import 'package:hive/hive.dart';

part 'anime_all.g.dart';

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

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

@HiveType(typeId: 0)
class AnimeAll extends HiveObject {
@HiveField(0)
final int id;

@HiveField(1)
final String title;

@HiveField(2)
final String altTitle;

@HiveField(3)
final int season;

@HiveField(4)
final int ongoing;

@HiveField(5)
final int hbId;

@HiveField(6)
final DateTime createdAt;

@HiveField(7)
final DateTime updatedAt;

@HiveField(8)
final int hidden;

@HiveField(9)
final int malId;

@HiveField(10)
final Slug slug;

AnimeAll({
this.id,
this.title,
this.altTitle,
this.season,
this.ongoing,
this.hbId,
this.createdAt,
this.updatedAt,
this.hidden,
this.malId,
this.slug,
});

factory AnimeAll.fromJson(Map<String, dynamic> json) => AnimeAll(
id: json["id"],
title: json["title"],
altTitle: json["alt_title"] == null ? null : json["alt_title"],
season: json["season"],
ongoing: json["ongoing"],
hbId: json["hb_id"] == null ? null : json["hb_id"],
createdAt: DateTime.parse(json["created_at"]),
updatedAt: DateTime.parse(json["updated_at"]),
hidden: json["hidden"],
malId: json["mal_id"] == null ? null : json["mal_id"],
slug: Slug.fromJson(json["slug"]),
);

Map<String, dynamic> toJson() => {
"id": id,
"title": title,
"alt_title": altTitle == null ? null : altTitle,
"season": season,
"ongoing": ongoing,
"hb_id": hbId == null ? null : hbId,
"created_at": createdAt.toIso8601String(),
"updated_at": updatedAt.toIso8601String(),
"hidden": hidden,
"mal_id": malId == null ? null : malId,
"slug": slug.toJson(),
};
}

class Slug {
Slug({
this.id,
this.slug,
this.animeId,
this.createdAt,
this.updatedAt,
});

int id;
String slug;
int animeId;
DateTime createdAt;
DateTime updatedAt;

factory Slug.fromJson(Map<String, dynamic> json) => Slug(
id: json["id"],
slug: json["slug"],
animeId: json["anime_id"],
createdAt: DateTime.parse(json["created_at"]),
updatedAt: DateTime.parse(json["updated_at"]),
);

Map<String, dynamic> toJson() => {
"id": id,
"slug": slug,
"anime_id": animeId,
"created_at": createdAt.toIso8601String(),
"updated_at": updatedAt.toIso8601String(),
};
}

模型/anime_all.g.dart

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'anime_all.dart';

// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************

class AnimeAllAdapter extends TypeAdapter<AnimeAll> {
@override
final int typeId = 0;

@override
AnimeAll read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = <int, dynamic>{
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return AnimeAll(
id: fields[0] as int,
title: fields[1] as String,
altTitle: fields[2] as String,
season: fields[3] as int,
ongoing: fields[4] as int,
hbId: fields[5] as int,
createdAt: fields[6] as DateTime,
updatedAt: fields[7] as DateTime,
hidden: fields[8] as int,
malId: fields[9] as int,
slug: fields[10] as Slug,
);
}

@override
void write(BinaryWriter writer, AnimeAll obj) {
writer
..writeByte(11)
..writeByte(0)
..write(obj.id)
..writeByte(1)
..write(obj.title)
..writeByte(2)
..write(obj.altTitle)
..writeByte(3)
..write(obj.season)
..writeByte(4)
..write(obj.ongoing)
..writeByte(5)
..write(obj.hbId)
..writeByte(6)
..write(obj.createdAt)
..writeByte(7)
..write(obj.updatedAt)
..writeByte(8)
..write(obj.hidden)
..writeByte(9)
..write(obj.malId)
..writeByte(10)
..write(obj.slug);
}

@override
int get hashCode => typeId.hashCode;

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is AnimeAllAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}

services/net/anime_twist.dart

import 'package:http/http.dart' as http;
import 'package:twist_go/models/anime_all.dart';
import 'package:twist_go/.env.dart';
import 'package:twist_go/models/anime_single.dart';
import 'package:twist_go/models/anime_sources.dart';

class AnimeTwistApiService {
static const String _baseUrl = 'https://twist.moe/api/anime/';
var _headers = {'x-access-token': EnvironmentVariables.token};

Future<List<AnimeAll>> getAllAnime() async {
final http.Response response = await http.get(
_baseUrl,
headers: _headers,
);

return animeAllFromJson(response.body);
}

Future<AnimeSingle> getSingleAnime(String slug) async {
final http.Response response = await http.get(
_baseUrl + slug,
headers: _headers,
);

return animeSingleFromJson(response.body);
}

Future<List<AnimeSources>> getAnimeSources(String slug) async {
final http.Response response = await http.get(
_baseUrl + slug + '/sources',
headers: _headers,
);

return animeSourcesFromJson(response.body);
}
}

services/db/anime_twist_cache.dart

import 'package:hive/hive.dart';
import 'package:twist_go/models/anime_all.dart';
import 'package:twist_go/services/net/anime_twist.dart';
import 'package:path_provider/path_provider.dart' as path_provider;

class AnimeTwistCacheService {
Future<void> cacheAllAnime() async {
final _appDocumentDir =
await path_provider.getApplicationDocumentsDirectory();
Hive.init(_appDocumentDir.path);
Hive.registerAdapter<AnimeAll>(AnimeAllAdapter());

Hive.openBox('all-anime');
final AnimeTwistApiService _animeTwistApiService = AnimeTwistApiService();

List<AnimeAll> allAnime = await _animeTwistApiService.getAllAnime();

Hive.box('all-anime').add(allAnime);
}
}

最佳答案

我已经解决了这里的问题,您有一个名为 Slug 的自定义数据类型

slug: fields[10] as Slug,

创建一个新的 dart 类并为 slug 创建一个适配器,就像你之前创建的那样:

class SlugAdapter extends TypeAdapter<Slug>{
@override
read(BinaryReader reader) {
// TODO: implement read
}

@override
// TODO: implement typeId
int get typeId => throw UnimplementedError();

@override
void write(BinaryWriter writer, obj) {
// TODO: implement write
}
}

然后在hive初始化时注册这个适配器:

Directory dir = await getApplicationDocumentsDirectory();
Hive
..init(dir.path)
..registerAdapter(SlugAdapter());

关于flutter - HiveError (HiveError : Cannot write, unknown type : Slug. Did you forget to register an adapter?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64080255/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com