Skip to content

Commit d0fe692

Browse files
authored
Merge pull request #71 from Flagsmith/fix/custom-storage
fix: Fix custom storage
2 parents 2e1c014 + 62fef89 commit d0fe692

2 files changed

Lines changed: 64 additions & 3 deletions

File tree

lib/src/flagsmith_client.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@ class FlagsmithClient {
166166
} else {
167167
switch (config.storageType) {
168168
case StorageType.custom:
169-
store = storage!;
169+
if (storage == null) {
170+
throw FlagsmithConfigException(Exception('When using StorageType.custom, a storage implementation must be provided'));
171+
}
172+
store = storage;
170173
break;
171174
default:
172175
store = InMemoryStorage();
@@ -181,7 +184,6 @@ class FlagsmithClient {
181184
///
182185
///
183186
Future<void> initialize() async {
184-
storageProvider = prepareStorage(storage: storage, config: config);
185187
await initStore(seeds: seeds);
186188
}
187189

@@ -198,7 +200,8 @@ class FlagsmithClient {
198200
config: config,
199201
apiKey: apiKey,
200202
seeds: seeds,
201-
)..storageProvider = prepareStorage(storage: storage, config: config);
203+
storage: storage,
204+
);
202205
await client.initStore(seeds: seeds);
203206
return client;
204207
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import 'package:flagsmith/flagsmith.dart';
2+
import 'package:test/test.dart';
3+
import '../shared.dart';
4+
5+
class TestStorage extends InMemoryStorage {
6+
bool initCalled = false;
7+
8+
@override
9+
Future<void> init() async {
10+
initCalled = true;
11+
return Future.value();
12+
}
13+
}
14+
15+
void main() {
16+
group('FlagsmithClient with custom storage', () {
17+
test('Should initialize with custom storage', () async {
18+
final customStorage = TestStorage();
19+
20+
// Initialize the client with custom storage
21+
final client = await FlagsmithClient.init(
22+
apiKey: apiKey,
23+
config: const FlagsmithConfig(
24+
storageType: StorageType.custom,
25+
isDebug: true,
26+
),
27+
storage: customStorage,
28+
seeds: <Flag>[
29+
Flag.seed('feature', enabled: true),
30+
],
31+
);
32+
33+
// Verify the client was initialized successfully
34+
expect(client, isA<FlagsmithClient>());
35+
expect(customStorage.initCalled, isTrue);
36+
37+
// Clean up
38+
client.close();
39+
});
40+
41+
test('Should throw error when StorageType.custom is used without providing storage', () async {
42+
expect(
43+
() => FlagsmithClient.init(
44+
apiKey: apiKey,
45+
config: const FlagsmithConfig(
46+
storageType: StorageType.custom,
47+
isDebug: true,
48+
),
49+
seeds: <Flag>[
50+
Flag.seed('feature', enabled: true),
51+
],
52+
),
53+
throwsA(isA<FlagsmithConfigException>()),
54+
);
55+
});
56+
57+
});
58+
}

0 commit comments

Comments
 (0)