@@ -28,6 +28,7 @@ import 'package:shared_preferences/shared_preferences.dart';
2828// | |
2929// |<---------------(init)------------------------|
3030// |----------------(init)----------------------->|
31+ // |<---------------(ack)------------------------>|
3132// | |
3233// |----------------(add)------------------------>|
3334// |<---------------(ack)-------------------------|
@@ -122,7 +123,6 @@ class SimpleDatabase {
122123 switch (command.code) {
123124 case _Codes .init:
124125 _sendPort = command.arg0 as SendPort ;
125- _completers.removeLast ().complete ();
126126 // ----------------------------------------------------------------------
127127 // Before using platform channels and plugins from background isolates we
128128 // need to register it with its root isolate. This is achieved by
@@ -161,13 +161,13 @@ class _SimpleDatabaseServer {
161161 _SimpleDatabaseServer (this ._sendPort);
162162
163163 final SendPort _sendPort;
164- String ? _path;
165- SharedPreferences ? _sharedPreferences;
164+ late final String _path;
165+ late final SharedPreferences _sharedPreferences;
166166
167167 // ----------------------------------------------------------------------
168168 // Here the plugin is used from the background isolate.
169169 // ----------------------------------------------------------------------
170- bool get _isDebug => _sharedPreferences? .getBool ('isDebug' ) ?? false ;
170+ bool get _isDebug => _sharedPreferences.getBool ('isDebug' ) ?? false ;
171171
172172 /// The main entrypoint for the background isolate sent to [Isolate.spawn] .
173173 static void _run (SendPort sendPort) {
@@ -200,9 +200,10 @@ class _SimpleDatabaseServer {
200200 // ----------------------------------------------------------------------
201201 BackgroundIsolateBinaryMessenger .ensureInitialized (rootIsolateToken);
202202 _sharedPreferences = await SharedPreferences .getInstance ();
203+ _sendPort.send (const _Command (_Codes .ack, arg0: null ));
203204 break ;
204205 case _Codes .add:
205- await _doAddEntry (command.arg0 as String );
206+ _doAddEntry (command.arg0 as String );
206207 break ;
207208 case _Codes .query:
208209 _doFind (command.arg0 as String );
@@ -213,15 +214,15 @@ class _SimpleDatabaseServer {
213214 }
214215
215216 /// Perform the add entry operation.
216- Future < void > _doAddEntry (String value) async {
217+ void _doAddEntry (String value) {
217218 if (_isDebug) {
218219 print ('Performing add: $value ' );
219220 }
220- File file = File (_path! );
221+ File file = File (_path);
221222 if (! file.existsSync ()) {
222223 file.createSync ();
223224 }
224- RandomAccessFile writer = await file.open (mode: FileMode .append);
225+ RandomAccessFile writer = file.openSync (mode: FileMode .append);
225226 List <int > bytes = utf8.encode (value);
226227 if (bytes.length > _entrySize) {
227228 bytes = bytes.sublist (0 , _entrySize);
@@ -232,17 +233,17 @@ class _SimpleDatabaseServer {
232233 }
233234 bytes = newBytes;
234235 }
235- await writer.writeFrom (bytes);
236- await writer.close ();
236+ writer.writeFromSync (bytes);
237+ writer.closeSync ();
237238 _sendPort.send (const _Command (_Codes .ack, arg0: null ));
238239 }
239240
240241 /// Perform the find entry operation.
241- Future < void > _doFind (String query) async {
242+ void _doFind (String query) {
242243 if (_isDebug) {
243244 print ('Performing find: $query ' );
244245 }
245- File file = File (_path! );
246+ File file = File (_path);
246247 if (file.existsSync ()) {
247248 RandomAccessFile reader = file.openSync ();
248249 List <int > buffer = List .filled (_entrySize, 0 );
0 commit comments