【问题标题】:Flutter SharedPreferences data limitFlutter SharedPreferences 数据限制
【发布时间】:2019-07-09 04:54:43
【问题描述】:

我正在尝试在 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();
      // ----------------------------------------------------------------------- //
    }

【问题讨论】:

  • 你能告诉我们你要保存什么样的对象吗?
  • 用数据结构编辑了我的问题。请评价一下。
  • 这不是一个大对象。据我所知,SharedPrefs 不能保存对象,它只能保存基本数据类型,如字符串、整数、布尔值等。我建议您将对象转换为某种 Json 字符串,然后将其保存在首选项中.
  • 是的,我这样做并保存了。我再次编辑了我的代码。
  • 是的,其实和缓存限制没有关系。我在这里出错了。 cart.toJsonMap()).toList();

标签: flutter sharedpreferences


【解决方案1】:

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

【讨论】:

  • 仅供参考:Integer.MAX_VALUE 是 2147483647
  • 如何计算我的对象的 Integer.MAX_VALUE ?
  • 您可以分享您保存到共享首选项的代码吗?
猜你喜欢
  • 2011-11-28
  • 1970-01-01
  • 2020-08-07
  • 2014-03-27
  • 1970-01-01
  • 2014-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多