Skip to content

Commit dc45306

Browse files
authored
Support all types of Dart ints in the setInt method for SharedPrefs (flutter#64)
1 parent acef365 commit dc45306

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

packages/shared_preferences/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [0.2.1] - 2017-05-17
2+
3+
* Support arbitrary length integers for setInt.
4+
15
## [0.2.0+1] - 2017-05-16
26

37
* Updated README

packages/shared_preferences/android/src/main/java/io/flutter/plugins/shared_preferences/SharedPreferencesPlugin.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.flutter.plugin.common.MethodChannel;
1111
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
1212
import io.flutter.plugin.common.PluginRegistry;
13+
import java.math.BigInteger;
1314
import java.util.HashMap;
1415
import java.util.HashSet;
1516
import java.util.List;
@@ -20,6 +21,8 @@ public class SharedPreferencesPlugin implements MethodCallHandler {
2021
private static final String SHARED_PREFERENCES_NAME = "FlutterSharedPreferences";
2122
private static final String CHANNEL_NAME = "plugins.flutter.io/shared_preferences";
2223

24+
private static final String BIG_INTEGER_PREFIX = "VGhpcyBpcyB0aGUgcHJlZml4IGZvciBCaWdJbnRlZ2Vy";
25+
2326
private final android.content.SharedPreferences preferences;
2427
private final android.content.SharedPreferences.Editor editor;
2528

@@ -40,7 +43,15 @@ private Map<String, Object> getAllPrefs() {
4043
Map<String, Object> filteredPrefs = new HashMap<>();
4144
for (String key : allPrefs.keySet()) {
4245
if (key.startsWith("flutter.")) {
43-
filteredPrefs.put(key, allPrefs.get(key));
46+
Object value = allPrefs.get(key);
47+
if (value instanceof String) {
48+
String stringValue = (String) value;
49+
if (stringValue.startsWith(BIG_INTEGER_PREFIX)) {
50+
String encoded = stringValue.substring(BIG_INTEGER_PREFIX.length());
51+
value = new BigInteger(encoded, Character.MAX_RADIX);
52+
}
53+
}
54+
filteredPrefs.put(key, value);
4455
}
4556
}
4657
return filteredPrefs;
@@ -59,7 +70,14 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
5970
result.success(null);
6071
break;
6172
case "setInt":
62-
editor.putInt(key, (int) call.argument("value")).apply();
73+
Number value = call.argument("value");
74+
if (value instanceof BigInteger) {
75+
BigInteger integerValue = (BigInteger) value;
76+
editor.putString(key, BIG_INTEGER_PREFIX + integerValue.toString(Character.MAX_RADIX));
77+
} else {
78+
editor.putLong(key, value.longValue());
79+
}
80+
editor.apply();
6381
result.success(null);
6482
break;
6583
case "setString":

packages/shared_preferences/ios/Classes/SharedPreferencesPlugin.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
2525
} else if ([method isEqualToString:@"setInt"]) {
2626
NSString *key = arguments[@"key"];
2727
NSNumber *value = arguments[@"value"];
28-
[[NSUserDefaults standardUserDefaults] setInteger:value.intValue forKey:key];
28+
// int type in Dart can come to native side in a variety of forms
29+
// It is best to store it as is and send it back when needed.
30+
// Platform channel will handle the conversion.
31+
[[NSUserDefaults standardUserDefaults] setValue:value forKey:key];
2932
result(nil);
3033
} else if ([method isEqualToString:@"setDouble"]) {
3134
NSString *key = arguments[@"key"];

packages/shared_preferences/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: shared_preferences
22

3-
version: 0.2.0+1
3+
version: 0.2.1
44
description: A Flutter plugin for reading and writing simple key-value pairs.
55
author: Flutter Team <flutter-dev@googlegroups.com>
66
homepage: https://github.com/flutter/plugins/tree/master/packages/shared_preferences

0 commit comments

Comments
 (0)