storage_handler.dart
1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import 'package:appframe/config/locator.dart';
import 'package:appframe/services/dispatcher.dart';
import 'package:shared_preferences/shared_preferences.dart';
class SetStorageSyncHandler extends MessageHandler {
@override
Future<dynamic> handleMessage(dynamic params) async {
if (params is! Map<String, dynamic>) {
throw Exception('参数错误');
}
var key = params['key'];
var value = params['value'];
if (key is! String || value is! String || key.isEmpty) {
throw Exception('参数错误');
}
try {
return await getIt.get<SharedPreferences>().setString(key, value);
} catch (e) {
throw Exception(e.toString());
}
}
}
class GetStorageSyncHandler extends MessageHandler {
@override
Future<dynamic> handleMessage(dynamic params) async {
if (params is! String || params.isEmpty) {
throw Exception('参数错误');
}
try {
return getIt.get<SharedPreferences>().getString(params) ?? "";
} catch (e) {
throw Exception(e.toString());
}
}
}
class ClearStorageSyncHandler extends MessageHandler {
@override
Future<dynamic> handleMessage(dynamic params) async {
return await getIt.get<SharedPreferences>().clear();
}
}