Flutter SharedPreferences数据限制

时间:2019-07-09 04:54:43

标签: flutter sharedpreferences

我正在尝试将一些数据保存在sharedPref中。但是似乎如果对象变大,那么sharedPref不会保存单个对象。有人可以告诉我Flutter应用程序的sharedPref中的大小限制吗?

这是对象的一部分,该对象已转换为Json并试图保存在sharedPref中。还保留了许多结构。

    I/flutter (13111):       products: {
    I/flutter (13111):         product: {
    I/flutter (13111):           id: 116
    I/flutter (13111):           metaMerchant: {
    I/flutter (13111):             id: 3
    I/flutter (13111):             version: 1
    I/flutter (13111):             brandDisplayName: Merchant: 2
    I/flutter (13111):             logoUrls: https://brandmark.io/logo-rank/random/pepsi.png
    I/flutter (13111):             coverPhotoUrls: https://cdn.pixabay.com/photo/2015/10/12/15/18/store-984393_960_720.jpg
    I/flutter (13111):             businessTypes: CLOTHS
    I/flutter (13111):             businessLocation: {
    I/flutter (13111):             }
    I/flutter (13111):             businessAddress: {
    I/flutter (13111):             }
    I/flutter (13111):             businessSchedule: {
    I/flutter (13111):             }
    I/flutter (13111):             isOpenNow: true
    I/flutter (13111):             deliveryCharge: 20.0
    I/flutter (13111):             merchantTabs: {
    I/flutter (13111):               tabId: home
    I/flutter (13111):               displayName: Home
    I/flutter (13111):               groupIds: 11
    I/flutter (13111):               groupIds: 12
    I/flutter (13111):               groupIds: 13
    I/flutter (13111):               groupIds: 14
    I/flutter (13111):               groupIds: 15
    I/flutter (13111):             }
    I/flutter (13111):             merchantTabs: {
    I/flutter (13111):               tabId: home1
    I/flutter (13111):               displayName: Home1
    I/flutter (13111):               groupIds: 11
    I/flutter (13111):               groupIds: 12
    I/flutter (13111):               groupIds: 13
    I/flutter (13111):               groupIds: 14
    I/flutter (13111):               groupIds: 15
    I/flutter (13111):             }
    I/flutter (13111):             merchantTabs: {
    I/flutter (13111):               tabId: home2
    I/flutter (13111):               displayName: Home2
    I/flutter (13111):               groupIds: 11
    I/flutter (13111):               groupIds: 12
    I/flutter (13111):               groupIds: 13
    I/flutter (13111):               groupIds: 14
    I/flutter (13111):               groupIds: 15
    I/flutter (13111):             }
    I/flutter (13111):             merchantTabs: {
    I/flutter (13111):               tabId: home3
    I/flutter (13111):               displayName: Home3
    I/flutter (13111):               groupIds: 11
    I/flutter (13111):               groupIds: 12
    I/flutter (13111):               groupIds: 13
    I/flutter (13111):               groupIds: 14
    I/flutter (13111):               groupIds: 15
    I/flutter (13111):             }
    I/flutter (13111):             merchantTabs: {
    I/flutter (13111):               tabId: home4
    I/flutter (13111):               displayName: Home4
    I/flutter (13111):               groupIds: 11
    I/flutter (13111):               groupIds: 12
    I/flutter (13111):               groupIds: 13
    I/flutter (13111):               groupIds: 14
    I/flutter (13111):               groupIds: 15
    I/flutter (13111):             }
    I/flutter (13111):             merchantTabs: {
    I/flutter (13111):               tabId: home5
    I/flutter (13111):               displayName: Home5
    I/flutter (13111):               groupIds: 11
    I/flutter (13111):               groupIds: 12
    I/flutter (13111):               groupIds: 13
    I/flutter (13111):               groupIds: 14
    I/flutter (13111):               groupIds: 15
    I/flutter (13111):             }
    I/flutter (13111):           }
    I/flutter (13111):           title: Product16
    I/flutter (13111):           originalPrice: 2962.0
    I/flutter (13111):           sellingPrice: 2962.0
    I/flutter (13111):           imageUrls: https://catseye.com.bd/media/catalog/product/cache/1/small_image/1362x1653/85e4522595efc69f496374d01ef2bf13/c/e/ce_panjabi_i14_5e2_623_8_tk_5990_1_.jpg
    I/flutter (13111):           deliveryCharge: 20.0
    I/flutter (13111):           productVariation: {
    I/flutter (13111):             variationEnable: true
    I/flutter (13111):             extensions: {
    I/flutter (13111):               id: 44228028569101
    I/flutter (13111):               title: Extension: 1
    I/flutter (13111):               merchantId: 3
    I/flutter (13111):               displayTitle: Extension: 1
    I/flutter (13111):               description: Extension 1
    I/flutter (13111):               extensionItems: {
    I/flutter (13111):                 id: 44228028548389
    I/flutter (13111):                 itemTitle: ExtensionItem: 1
    I/flutter (13111):                 extraCharge: 98.0
    I/flutter (13111):               }
    I/flutter (13111):               extensionItems: {
    I/flutter (13111):                 id: 44228028557460
    I/flutter (13111):                 itemTitle: ExtensionItem: 2
    I/flutter (13111):                 extraCharge: 42.0
    I/flutter (13111):               }
    I/flutter (13111):               extensionItems: {
    I/flutter (13111):                 id: 44228028560873
    I/flutter (13111):                 itemTitle: ExtensionItem: 3
    I/flutter (13111):                 extraCharge: 52.0
    I/flutter (13111):               }
    I/flutter (13111):               extensionItems: {
    I/flutter (13111):                 id: 44228028563518
    I/flutter (13111):                 itemTitle: ExtensionItem: 4
    I/flutter (13111):                 extraCharge: 40.0
    I/flutter (13111):               }
    I/flutter (13111):             }
    I/flutter (13111):             extensions: {
    I/flutter (13111):               id: 44228028605392
    I/flutter (13111):               title: Extension: 2
    I/flutter (13111):               merchantId: 3
    I/flutter (13111):               displayTitle: Extension: 2
    I/flutter (13111):               description: Extension 2
    I/flutter (13111):               extensionItems: {
    I/flutter (13111):                 id: 44228028589832
    I/flutter (13111):                 itemTitle: ExtensionItem: 1
    I/flutter (13111):                 extraCharge: 86.0
    I/flutter (13111):               }
    I/flutter (13111):               extensionItems: {
    I/flutter (13111):                 id: 44228028593236
    I/flutter (13111):                 itemTitle: ExtensionItem: 2
    I/flutter (13111):                 extraCharge: 57.0
    I/flutter (13111):               }
    I/flutter (13111):               extensionItems: {
    I/flutter (13111):                 id: 44228028599177
    I/flutter (13111):                 itemTitle: ExtensionItem: 3
    I/flutter (13111):                 extraCharge: 34.0
    I/flutter (13111):               }
    I/flutter (13111):               extensionItems: {
    I/flutter (13111):                 id: 44228028602233
    I/flutter (13111):                 itemTitle: ExtensionItem: 4
    I/flutter (13111):                 extraCharge: 28.0
    I/flutter (13111):               }
    I/flutter (13111):             }
    I/flutter (13111):             itemCombinations: {
    I/flutter (13111):               indexIds: 0
    I/flutter (13111):               indexIds: 0
    I/flutter (13111):               combinationPrice: 4409.0
    I/flutter (13111):             }
    I/flutter (13111):             itemCombinations: {
    I/flutter (13111):               indexIds: 0
    I/flutter (13111):               indexIds: 1
    I/flutter (13111):               combinationPrice: 3706.0
    I/flutter (13111):             }
    I/flutter (13111):             itemCombinations: {
    I/flutter (13111):               indexIds: 0
    I/flutter (13111):               indexIds: 2
    I/flutter (13111):               combinationPrice: 5198.0
    I/flutter (13111):             }
    I/flutter (13111):             itemCombinations: {
    I/flutter (13111):               indexIds: 0
    I/flutter (13111):               indexIds: 3
    I/flutter (13111):               combinationPrice: 1818.0
    I/flutter (13111):             }
    I/flutter (13111):             itemCombinations: {
    I/flutter (13111):               indexIds: 1
    I/flutter (13111):               indexIds: 0
    I/flutter (13111):               combinationPrice: 942.0
    I/flutter (13111):             }
    I/flutter (13111):             itemCombinations: {
    I/flutter (13111):               indexIds: 1
    I/flutter (13111):               indexIds: 1
    I/flutter (13111):               combinationPrice: 5908.0
    I/flutter (13111):             }
    I/flutter (13111):             itemCombinations: {
    I/flutter (13111):               indexIds: 1
    I/flutter (13111):               indexIds: 2
    I/flutter (13111):               combinationPrice: 1542.0
    I/flutter (13111):             }
    I/flutter (13111):             itemCombinations: {
    I/flutter (13111):               indexIds: 1
    I/flutter (13111):               indexIds: 3
    I/flutter (13111):               combinationPrice: 6495.0
    I/flutter (13111):             }
    I/flutter (13111):             itemCombinations: {
    I/flutter (13111):               indexIds: 2
    I/flutter (13111):               indexIds: 0
    I/flutter (13111):               combinationPrice: 8357.0
    I/flutter (13111):             }
    I/flutter (13111):             itemCombinations: {
    I/flutter (13111):               indexIds: 2
    I/flutter (13111):               indexIds: 1
    I/flutter (13111):               combinationPrice: 8865.0
    I/flutter (13111):             }
    I/flutter (13111):             itemCombinations: {
    I/flutter (13111):               indexIds: 2
    I/flutter (13111):               indexIds: 2
    I/flutter (13111):               combinationPrice: 3223.0
    I/flutter (13111):             }
    I/flutter (13111):             itemCombinations: {
    I/flutter (13111):               indexIds: 2
    I/flutter (13111):               indexIds: 3
    I/flutter (13111):               combinationPrice: 9546.0
    I/flutter (13111):             }
    I/flutter (13111):             itemCombinations: {
    I/flutter (13111):               indexIds: 3
    I/flutter (13111):               indexIds: 0
    I/flutter (13111):               combinationPrice: 4137.0
    I/flutter (13111):             }
    I/flutter (13111):             itemCombinations: {
    I/flutter (13111):               indexIds: 3
    I/flutter (13111):               indexIds: 1
    I/flutter (13111):               combinationPrice: 5253.0
    I/flutter (13111):             }
    I/flutter (13111):             itemCombinations: {
    I/flutter (13111):               indexIds: 3
    I/flutter (13111):               indexIds: 2
    I/flutter (13111):               combinationPrice: 1503.0
    I/flutter (13111):             }
    I/flutter (13111):             itemCombinations: {
    I/flutter (13111):               indexIds: 3
    I/flutter (13111):               indexIds: 3
    I/flutter (13111):               combinationPrice: 2174.0
    I/flutter (13111):             }
    I/flutter (13111):           }
    I/flutter (13111):         }
    I/flutter (13111):       }

这是将数据发送到sharedPref的代码:

  Future<void> saveCarts(Iterable<CartOrderItem> items) async {
    try {
      final list = items.map((cart) => cart.toJsonMap()).toList();
      await Preferences.instance.setJson(CART_LIST_KEY, list);
    } catch (err) {
      print('<!> [CartRepository] saveCart(): $err');
    }
  }

这是sharedPref的保存位置:

    import 'dart:async';
    import 'dart:convert' show json;
    import 'package:shared_preferences/shared_preferences.dart';

    export 'package:hurryup/src/strings.dart';

    class Preferences {
      Future<SharedPreferences> source() {
        return SharedPreferences.getInstance();
      }

      Future<int> getInt(String key) async {
        final pref = await source();
        return pref.getInt(key);
      }

      Future<void> setInt(String key, int value) async {
        final pref = await source();
        final result = await pref.setInt(key, value);
        if (!result) {
          throw Exception('Could not store integer');
        }
      }

      Future<String> getString(String key) async {
        final pref = await source();
        return pref.getString(key);
      }

      Future<void> setString(String key, String value) async {
        final pref = await source();
        final result = await pref.setString(key, value);
        if (!result) {
          throw Exception('Could not store string');
        }
      }

      Future<dynamic> getJson(String key) async {
        String source = await getString(key);
        if (source == null) return null;
        return json.decode(source);
      }

      Future<void> setJson(String key, dynamic jsonData) async {
        String value;
        if (jsonData != null) {
          value = json.encode(jsonData);
        }
        await setString(key, value);
      }

      // ----------------------------------------------------------------------- //
      static Preferences _instance;
      static Preferences get instance => _instance ??= Preferences();
      // ----------------------------------------------------------------------- //
    }

1 个答案:

答案 0 :(得分:0)

如果您存储的string超出了Integer.MAX_VALUE,最好将其保存在database中。