3
3
// found in the LICENSE file.
4
4
5
5
import 'dart:async' ;
6
+ import 'dart:io' ;
6
7
7
8
import 'package:flutter/services.dart' ;
8
9
import 'package:meta/meta.dart' ;
@@ -17,30 +18,41 @@ const MethodChannel _kChannel =
17
18
class SharedPreferences {
18
19
SharedPreferences ._(this ._preferenceCache, {@required this .filename});
19
20
21
+ /// Default file under which preferences are stored.
22
+ static const String defaultFilename = 'FlutterSharedPreferences' ;
20
23
static const String _prefix = 'flutter.' ;
21
24
static final Map <String , Future <SharedPreferences >> _openedInstances =
22
25
< String , Future <SharedPreferences >> {};
23
26
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
+
24
36
/// Returns an instance of [SharedPreferences]
25
37
/// with values corresponding to those stored under the file with the specified [filename] .
26
38
///
39
+ /// If a file with the specified [filename] doesn't already exist, it will automatically be created.
40
+ /// The [filename] cannot be null.
41
+ ///
27
42
/// Because this is reading from disk, it shouldn't be awaited in
28
43
/// performance-sensitive blocks.
29
44
///
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 .
32
47
///
33
48
/// The values in [SharedPreferences] are cached.
34
49
/// A new instance is actually created only the first time this method is called with the specified [filename] .
35
50
///
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 {
43
54
ArgumentError .checkNotNull (filename);
55
+ assert (filename == defaultFilename || Platform .isAndroid);
44
56
try {
45
57
return await _openedInstances.putIfAbsent (filename, () async {
46
58
final Map <String , Object > preferencesMap =
0 commit comments