doesn't persists custom objects after restart
- Dominant language
- Dart
- Stars
- 4.4k
- Forks
- 449
- PR merge metrics
- No merged PRs in 30d
Description
i'm sotring custom `List` this way `put(k, v)` in a hive box. after putting the data i'm able to access it. but when i restart the app data disappears.
custom object class:
```
@HiveType(typeId: 3)
class FakePerson {
@HiveField(0)
final String firstName;
@HiveField(1)
final String lastName;
@HiveField(2)
final String email;
@HiveField(3)
final String gender;
@HiveField(4)
final String phoneNumber;
FakePerson({
required this.firstName,
required this.lastName,
required this.email,
required this.gender,
required this.phoneNumber,
});
Map toMap() {
return {
'first_name': firstName,
'last_name': lastName,
'email': email,
'gender': gender,
'phone_number': phoneNumber,
};
}
factory FakePerson.fromMap(Map map) {
return FakePerson(
firstName: map['first_name'] as String,
lastName: map['last_name'] as String,
email: map['email'] as String,
gender: map['gender'] as String,
phoneNumber: map['phone_number'] as String,
);
}
String toJson() => json.encode(toMap());
factory FakePerson.fromJson(String source) =>
FakePerson.fromMap(json.decode(source) as Map);
}
```
in main.dart
```
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final directory = await getApplicationSupportDirectory();
Hive.initFlutter(directory.path);
Hive.registerAdapter(FakePersonAdapter());
await Hive.openBox>('fake_person_list',
path: directory.path, crashRecovery: true);
// await BoxCollection.open('APP_DB', {'FAKE_PERSON_LIST'},
// path: directory.path);
runApp(
const MyApp(),
);
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State createState() => _MyAppState();
}
class _MyAppState extends State {
late final fakePersonBox;
Future> getFakePersonList() async {
final String response = await rootBundle.loadString('assets/users.json');
final data = await json.decode(response);
return List.from(
data.map(
(dynamic person) => FakePerson.fromMap(person),
),
);
}
void insert() async {
List fakePersonList = await getFakePersonList();
fakePersonBox.put('latest', fakePersonList);
debugPrint(fakePersonBox.get('latest')[0].firstName);
debugPrint(fakePersonBox.toMap().toString());
}
@override
void initState() {
debugPrint("Initstate Called");
fakePersonBox = Hive.box>('fake_person_list');
insert();
super.initState();
}
@override
void dispose() {
fakePersonBox.close();
super.dispose();
}
}
```
after reboot if i comment out first 2 line inside insert() function i get the following exception,
`Exception has occurred._CastError (type 'List' is not a subtype of type 'List?' in type cast)`
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the Hive.openBox call and the FakePersonAdapter registration shown in main.dart, then reproduce the put, close, restart, and typed get sequence. Compare the persisted value's runtime type with the List expectation and check whether an existing Hive regression test covers typed lists. Done means the custom-object list can be read with its intended type after restart, with a regression test or documented reproduction.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart, flutter
- Domain
- database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100