This repository was archived by the owner on Jul 9, 2020. It is now read-only.
File tree 4 files changed +41
-1
lines changed
4 files changed +41
-1
lines changed Original file line number Diff line number Diff line change
1
+ # 0.1.2
2
+
3
+ - fix: reintroduce migration code to ensure no data loss ([ #67 ] ( https://github.com/felangel/hydrated_bloc/issues/67 ) )
4
+
1
5
# 0.1.1
2
6
3
7
- fix: support use alongside hive ([ #53 ] ( https://github.com/felangel/cubit/pull/53 ) )
Original file line number Diff line number Diff line change 1
1
import 'dart:async' ;
2
+ import 'dart:convert' ;
2
3
import 'dart:io' ;
3
4
import 'package:flutter/foundation.dart' ;
4
5
import 'package:hive/hive.dart' ;
@@ -62,10 +63,32 @@ class HydratedStorage implements Storage {
62
63
'hydrated_box' ,
63
64
encryptionCipher: encryptionCipher,
64
65
);
66
+
67
+ await _migrate (directory, box);
68
+
65
69
return _instance = HydratedStorage (box);
66
70
});
67
71
}
68
72
73
+ static Future _migrate (Directory directory, Box box) async {
74
+ final file = File ('${directory .path }/.hydrated_bloc.json' );
75
+ if (await file.exists ()) {
76
+ try {
77
+ final dynamic storageJson = json.decode (await file.readAsString ());
78
+ final cache = (storageJson as Map ).cast <String , String >();
79
+ for (final key in cache.keys) {
80
+ try {
81
+ final string = cache[key];
82
+ final dynamic object = json.decode (string);
83
+ await box.put (key, object);
84
+ } on dynamic catch (_) {}
85
+ }
86
+ } on dynamic catch (_) {} finally {
87
+ await file.delete ();
88
+ }
89
+ }
90
+ }
91
+
69
92
/// Internal instance of [HiveImpl] .
70
93
/// It should only be used for testing.
71
94
@visibleForTesting
Original file line number Diff line number Diff line change 1
1
name : hydrated_cubit
2
2
description : An extension to the cubit state management library which automatically persists and restores cubit states.
3
- version : 0.1.1
3
+ version : 0.1.2
4
4
homepage : https://github.com/felangel/cubit
5
5
6
6
environment :
Original file line number Diff line number Diff line change
1
+ import 'dart:convert' ;
1
2
import 'dart:io' ;
2
3
3
4
import 'package:flutter/services.dart' ;
@@ -33,6 +34,18 @@ void main() {
33
34
await storage? .clear ();
34
35
});
35
36
37
+ group ('migration' , () {
38
+ test ('returns correct value when file exists' , () async {
39
+ final directory = await getTemporaryDirectory ();
40
+ File ('${directory .path }/.hydrated_bloc.json' )
41
+ ..writeAsStringSync (json.encode ({
42
+ 'CounterBloc' : json.encode ({'value' : 4 })
43
+ }));
44
+ storage = await HydratedStorage .build ();
45
+ expect (storage.read ('CounterBloc' )['value' ] as int , 4 );
46
+ });
47
+ });
48
+
36
49
group ('build' , () {
37
50
setUp (() async {
38
51
await (await HydratedStorage .build ()).clear ();
You can’t perform that action at this time.
0 commit comments