Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 2388916

Browse files
committed
[shared_preferences] Create a new method to get an instance with a file on Android
1 parent c8a8384 commit 2388916

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

packages/shared_preferences/shared_preferences/example/test_driver/shared_preferences_e2e.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ void main() {
3030
SharedPreferences preferences2;
3131

3232
setUp(() async {
33-
preferences1 = await SharedPreferences.getInstance(filename: filename1);
34-
preferences2 = await SharedPreferences.getInstance(filename: filename2);
33+
preferences1 = await SharedPreferences.getInstanceForFile(filename: filename1);
34+
preferences2 = await SharedPreferences.getInstanceForFile(filename: filename2);
3535
});
3636

3737
tearDown(() {

packages/shared_preferences/shared_preferences/lib/shared_preferences.dart

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'dart:async';
6+
import 'dart:io';
67

78
import 'package:flutter/services.dart';
89
import 'package:meta/meta.dart';
@@ -17,30 +18,41 @@ const MethodChannel _kChannel =
1718
class SharedPreferences {
1819
SharedPreferences._(this._preferenceCache, {@required this.filename});
1920

21+
/// Default file under which preferences are stored.
22+
static const String defaultFilename = 'FlutterSharedPreferences';
2023
static const String _prefix = 'flutter.';
2124
static final Map<String, Future<SharedPreferences>> _openedInstances =
2225
<String, Future<SharedPreferences>>{};
2326

27+
/// Returns an instance of [SharedPreferences] with the default file.
28+
///
29+
/// Because this is reading from disk, it shouldn't be awaited in
30+
/// performance-sensitive blocks.
31+
///
32+
/// The values in [SharedPreferences] are cached.
33+
/// A new instance is actually created only the first time this method is called with the specified [filename].
34+
static Future<SharedPreferences> getInstance() async => getInstanceForFile();
35+
2436
/// Returns an instance of [SharedPreferences]
2537
/// with values corresponding to those stored under the file with the specified [filename].
2638
///
39+
/// If a file with the specified [filename] doesn't already exist, it will automatically be created.
40+
/// The [filename] cannot be null.
41+
///
2742
/// Because this is reading from disk, it shouldn't be awaited in
2843
/// performance-sensitive blocks.
2944
///
30-
/// WARNING: [filename] argument for now only works on Android.
31-
/// On iOs, the default name will always be used, even with different value in parameter.
45+
/// **WARNING**: this method for now only works on Android.
46+
/// On iOS, use the [getInstance] method, otherwise an [AssertionError] will be thrown.
3247
///
3348
/// The values in [SharedPreferences] are cached.
3449
/// A new instance is actually created only the first time this method is called with the specified [filename].
3550
///
36-
/// If a file with the specified [filename] doesn't already exist, it will automatically be created.
37-
/// The [filename] cannot be null ; otherwise an [ArgumentError] will be thrown.
38-
/// The default value of [filename] is the name of the file used in the previous version of this plugin.
39-
///
40-
/// For Android, see https://developer.android.com/training/data-storage/shared-preferences.html for more details on the platform implementation.
41-
static Future<SharedPreferences> getInstance(
42-
{String filename = "FlutterSharedPreferences"}) async {
51+
/// See https://developer.android.com/training/data-storage/shared-preferences.html for more details on the platform implementation.
52+
static Future<SharedPreferences> getInstanceForFile(
53+
{String filename = defaultFilename}) async {
4354
ArgumentError.checkNotNull(filename);
55+
assert(filename == defaultFilename || Platform.isAndroid);
4456
try {
4557
return await _openedInstances.putIfAbsent(filename, () async {
4658
final Map<String, Object> preferencesMap =

packages/shared_preferences/shared_preferences/test/shared_preferences_test.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ void main() {
4343
}
4444
return null;
4545
});
46-
preferences = await SharedPreferences.getInstance(filename: filenameTest);
46+
preferences =
47+
await SharedPreferences.getInstanceForFile(filename: filenameTest);
4748
log.clear();
4849
});
4950

@@ -183,7 +184,7 @@ void main() {
183184
test('test 1', () async {
184185
preferences.setMockInitialValues(<String, dynamic>{_key: 'my string'});
185186
final SharedPreferences prefs =
186-
await SharedPreferences.getInstance(filename: filenameTest);
187+
await SharedPreferences.getInstanceForFile(filename: filenameTest);
187188
final String value = prefs.getString(_key);
188189
expect(value, 'my string');
189190
});
@@ -192,7 +193,7 @@ void main() {
192193
preferences.setMockInitialValues(
193194
<String, dynamic>{_prefixedKey: 'my other string'});
194195
final SharedPreferences prefs =
195-
await SharedPreferences.getInstance(filename: filenameTest);
196+
await SharedPreferences.getInstanceForFile(filename: filenameTest);
196197
final String value = prefs.getString(_key);
197198
expect(value, 'my other string');
198199
});

0 commit comments

Comments
 (0)