33// found in the LICENSE file.
44
55import 'dart:async' ;
6+ import 'dart:io' ;
67
78import 'package:flutter/services.dart' ;
89import 'package:meta/meta.dart' ;
@@ -17,30 +18,41 @@ const MethodChannel _kChannel =
1718class 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 =
0 commit comments