Skip to content

Commit 2a9d898

Browse files
committed
goderbauer feedback 3
1 parent 6d90d9d commit 2a9d898

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

background_isolate_channels/lib/main.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ class _MyHomePageState extends State<MyHomePage> {
8585
SimpleDatabase.open(dbPath).then((SimpleDatabase database) {
8686
setState(() {
8787
_database = database;
88-
_refresh();
8988
});
89+
_refresh();
9090
});
9191
});
9292
super.initState();
@@ -104,7 +104,7 @@ class _MyHomePageState extends State<MyHomePage> {
104104
if (query != null) {
105105
_query = query;
106106
}
107-
_database?.find(_query).toList().then((entries) {
107+
_database!.find(_query).toList().then((entries) {
108108
setState(() {
109109
_entries = entries;
110110
});
@@ -129,7 +129,8 @@ class _MyHomePageState extends State<MyHomePage> {
129129
body: Column(
130130
children: [
131131
TextField(
132-
onChanged: (query) => _refresh(query: query),
132+
onChanged:
133+
_database == null ? null : (query) => _refresh(query: query),
133134
decoration: const InputDecoration(
134135
labelText: 'Search',
135136
suffixIcon: Icon(Icons.search),
@@ -146,7 +147,7 @@ class _MyHomePageState extends State<MyHomePage> {
146147
],
147148
),
148149
floatingActionButton: FloatingActionButton(
149-
onPressed: _addDate,
150+
onPressed: _database == null ? null : _addDate,
150151
tooltip: 'Add',
151152
child: const Icon(Icons.add),
152153
),

background_isolate_channels/lib/simple_database.dart

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)