I am developing an application in which it is necessary to save user metrics locally in the settings window. I decided to use Hive for this purpose. Generated and registered the adapter. I opened the box, I'm trying to use .put
, but I get an error:
E/flutter (19700): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: HiveError: Cannot write, unknown type: Settings. Did you forget to register an adapter?
E/flutter (19700): #0 BinaryWriterImpl.write (package:hive/src/binary/binary_writer_impl.dart:338:9)
E/flutter (19700): #1 BinaryWriterImpl.writeFrame (package:hive/src/binary/binary_writer_impl.dart:282:9)
E/flutter (19700): #2 StorageBackendVm.writeFrames.<anonymous closure> (package:hive/src/backend/vm/storage_backend_vm.dart:128:31)
E/flutter (19700): #3 ReadWriteSync.syncWrite.<anonymous closure> (package:hive/src/backend/vm/read_write_sync.dart:26:41)
E/flutter (19700): <asynchronous suspension>
E/flutter (19700): #4 _AsyncCompleter.complete (dart:async/future_impl.dart:41:3)
E/flutter (19700): <asynchronous suspension>
E/flutter (19700): #5 BoxImpl._writeFrames (package:hive/src/box/box_impl.dart:88:7)
E/flutter (19700): <asynchronous suspension>
E/flutter (19700):
My code:
Main.dart
import 'package:diabet_diary/app.dart';
import 'package:diabet_diary/features/Diary/widgets/table.dart';
import 'package:diabet_diary/features/Settings/Settings.dart';
import 'package:flutter/material.dart';
import 'package:hive_flutter/hive_flutter.dart';
void main() async {
await Hive.initFlutter();
Hive.registerAdapter(DiaryEntryAdapter());
Hive.registerAdapter(SettingsAdapter());
runApp(const DiabetDiaty());
// ignore: unused_local_variable
final diabetEntryBox = await Hive.openBox<DiaryEntry>('diabet_entry_box');
// ignore: unused_local_variable
final settingsBox = await Hive.openBox('settings_box');
}
Settings_screen.dart
import 'package:flutter/material.dart';
import 'package:diabet_diary/features/MyHomePage/widgets/button_style.dart';
import 'package:hive_flutter/hive_flutter.dart';
part 'Settings_screen.g.dart';
final gkMaxController = TextEditingController();
final gkMinController = TextEditingController();
final fchiController = TextEditingController();
final pathController = TextEditingController();
var settings = Settings(gkMax: '', gkMin: '', fchi: '', path: '');
Box settingsBox = Hive.box('settings_box');
const textstile1 = TextStyle(
color: Colors.black,
fontSize: 23,
);
@HiveType(typeId: 2)
class Settings {
@HiveField(0)
final String gkMax;
@HiveField(1)
final String gkMin;
@HiveField(2)
final String fchi;
@HiveField(3)
final String path;
Settings(
{required this.gkMax,
required this.gkMin,
required this.fchi,
required this.path});
}
class TextLeft extends StatelessWidget {
final String text;
const TextLeft({super.key, required this.text});
@override
Widget build(BuildContext context) {
return Align(
alignment: Alignment.centerLeft, child: Text(text, style: textstile1));
}
}
class SettingsScreen extends StatelessWidget {
const SettingsScreen({super.key});
@override
Widget build(BuildContext context) {
//settings = settingsBox.get(0);
gkMaxController.text = settings.gkMax;
gkMinController.text = settings.gkMin;
fchiController.text = settings.fchi;
pathController.text = settings.path;
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('Настройки')),
resizeToAvoidBottomInset: false,
body: Column(children: [
Padding(
padding: const EdgeInsets.only(left: 0),
child: Column(
children: [
SizedBox(
width: 300,
child: Column(children: [
const TextLeft(text: 'Максимальный нормальный уровень ГК'),
TextField(
controller: gkMaxController,
decoration: InputDecoration(
border: const OutlineInputBorder(),
hintText: 'Enter a search term',
),
)
]),
),
SizedBox(
width: 300,
child: Column(children: [
TextLeft(text: 'Минимальный нормальный уровень ГК'),
TextField(
controller: gkMinController,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'Enter a search term',
),
)
]),
),
SizedBox(
width: 300,
child: Column(children: [
TextLeft(text: 'ФЧИ'),
TextField(
controller: fchiController,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'Enter a search term',
),
)
]),
),
SizedBox(
width: 300,
child: Column(children: [
TextLeft(text: 'Путь сохранения дневника'),
TextField(
controller: pathController,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'Enter a search term',
),
)
]),
),
Padding(
padding: const EdgeInsets.only(top: 120),
child: ElevatedButton(
style: buttonstylediary,
onPressed: () {
if (gkMaxController.text.isEmpty) {
gkMaxController.text = ' ';
}
if (gkMinController.text.isEmpty) {
gkMinController.text = ' ';
}
if (fchiController.text.isEmpty) {
fchiController.text = ' ';
}
if (pathController.text.isEmpty) {
pathController.text = ' ';
}
settings = Settings(
gkMax: gkMaxController.text,
gkMin: gkMinController.text,
fchi: fchiController.text,
path: pathController.text,
);
settingsBox.put(0, settings);
},
child: const Column(
mainAxisSize: MainAxisSize.min,
children: [Icon(Icons.save), Text("Сохранить")])),
),
],
),
),
]));
}
}
Settings_screen.g.dart
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'Settings_screen.dart';
// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************
class SettingsAdapter extends TypeAdapter<Settings> {
@override
final int typeId = 2;
@override
Settings read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = <int, dynamic>{
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return Settings(
gkMax: fields[0] as String,
gkMin: fields[1] as String,
fchi: fields[2] as String,
path: fields[3] as String,
);
}
@override
void write(BinaryWriter writer, Settings obj) {
writer
..writeByte(4)
..writeByte(0)
..write(obj.gkMax)
..writeByte(1)
..write(obj.gkMin)
..writeByte(2)
..write(obj.fchi)
..writeByte(3)
..write(obj.path);
}
@override
int get hashCode => typeId.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is SettingsAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}
I need to save the metrics and then use them to output to TextField using controllers.
The problem is solved. Renaming the Settings class to SettingsSave helped. I can't understand why the error appeared in principle.