|
1 | 1 | import 'package:flutter/foundation.dart'; |
2 | 2 | import 'package:hive_ce/hive.dart'; |
3 | 3 |
|
| 4 | +import '../const/app.dart'; |
4 | 5 | import '../utils/app_data_dir/app_data_dir.dart'; |
| 6 | +import '../utils/same_types.dart'; |
5 | 7 | import 'theme_service.dart'; |
6 | 8 | import 'theme_service_hive_adapters.dart'; |
7 | 9 |
|
@@ -105,14 +107,32 @@ class ThemeServiceHive implements ThemeService { |
105 | 107 | @override |
106 | 108 | Future<T> load<T>(String key, T defaultValue) async { |
107 | 109 | try { |
108 | | - final dynamic value = _hiveBox.get(key, defaultValue: defaultValue); |
| 110 | + final dynamic value = await _hiveBox.get(key, defaultValue: defaultValue); |
109 | 111 | if (_debug) { |
110 | 112 | debugPrint('Hive LOAD _______________'); |
111 | | - debugPrint(' Type expected: $key as ${defaultValue.runtimeType}'); |
112 | | - debugPrint(' Type loaded : $key as ${value.runtimeType}'); |
113 | | - debugPrint(' Value loaded : $value'); |
| 113 | + debugPrint(' Store key : $key'); |
| 114 | + debugPrint(' Type expected : $T'); |
| 115 | + debugPrint(' Stored value : $value'); |
| 116 | + debugPrint(' Stored type : ${value.runtimeType}'); |
| 117 | + debugPrint(' Default value : $defaultValue'); |
| 118 | + debugPrint(' Default type : ${defaultValue.runtimeType}'); |
| 119 | + } |
| 120 | + // Add workaround for Hive WASM returning double instead of int, when |
| 121 | + // values saved to IndexedDb were int. |
| 122 | + // See issue: https://github.com/IO-Design-Team/hive_ce/issues/46 |
| 123 | + if (App.isRunningWithWasm && |
| 124 | + value != null && |
| 125 | + (value is double) && |
| 126 | + (sameTypes<T, int?>() || sameTypes<T, int>())) { |
| 127 | + final T loaded = value.round() as T; |
| 128 | + if (_debug) { |
| 129 | + debugPrint(' ** WASM Issue : Expected int but got double, ' |
| 130 | + 'returning as int: $loaded'); |
| 131 | + } |
| 132 | + return loaded; |
| 133 | + } else { |
| 134 | + return value as T; |
114 | 135 | } |
115 | | - return value as T; |
116 | 136 | } catch (e) { |
117 | 137 | debugPrint('Hive load (get) ERROR'); |
118 | 138 | debugPrint(' Error message ...... : $e'); |
|
0 commit comments