I'm trying to open the box separately because the box contains so much data that it's causing my main UI to stutter/freeze. Is there a way to open the box separately and return the box object?
I asked ChatGPT and he gave this answer:
Future<Box> openBoxInIsolate(String boxName, String path) async {
final receivePort = ReceivePort();
await Isolate.spawn(
_openBoxInIsolate,
IsolateArgument(boxName: boxName, path: path, sendPort: receivePort.sendPort),
);
return await receivePort.first as Box;
}
void _openBoxInIsolate(IsolateArgument arguments) async {
final box = await Hive.openBox(arguments.boxName, path: arguments.path);
arguments.sendPort.send(box);
}
class IsolateArgument {
final String boxName;
final String path;
final SendPort sendPort;
IsolateArgument({required this.boxName, required this.path, required this.sendPort});
}
But this code gives an error Invalid argument(s): Illegal argument in isolate message: (object extends NativeWrapper - Library:'dart:io' Class: _RandomAccessFileOpsImpl@13069316)
at arguments.sendPort.send(box);
line.