搜索插件、代码仓与 APP 分类

插件详情v2.0.10

native_shared_preferences

此包是 shared_preferences 包的副本,但键名中没有前缀。用于从之前的原生应用迁移数据。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
180
喜欢数
34
Pub 分数
130

安装插件

flutter pub add native_shared_preferences

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Flutter/Dart 包,作为轻量级、持久化的键值存储解决方案,与 shared_preferences 包功能相同,但没有键前缀。专为从旧版原生应用程序迁移数据而设计,通过保留原始键名,确保与现有存储数据的无缝兼容性。适用于需要简单、跨平台数据持久化且无额外开销的应用程序。该包支持所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux。

适合场景

  • 从原生应用迁移数据
  • 存储用户偏好
  • 在会话之间持久化应用状态
  • 跨平台数据存储
  • 简单的键值数据管理

主要特点

  • 无密钥前缀
  • 直接数据迁移支持
  • 多平台兼容性
  • 轻便且快速
  • 与现有代码的轻松集成

适合对象

  • 旧版应用迁移
  • 简单数据持久化需求
  • 跨平台应用
  • 需要直接密钥访问的开发者
  • 应用避免关键命名空间冲突

使用建议

  • 不建议用于大型数据集
  • 仅限基本的键值存储
  • 无内置加密
  • 需要仔细命名密钥
  • 依赖于底层平台存储机制

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Migration Shared Preferences

===========

Codemagic build status

This plugin is a copy of the shared_preferences package but without the prefix in the keys. Is used to migrate the data from previous native app.

The issue is that flutter add a prefix to the keys when read and write. So we can not read our old keys. Also for Android you can now specify name of your resource file in android/app/src/main/res/values/strings.xml:

<resources>
    ...
    <string name="flutter_shared_pref_name">resource_name</string>
    ...
</resources>

ISSUE https://github.com/flutter/flutter/issues/52544

Only use this plugin when is needed get the shared preferences from previous native app version. For other situation use the official shared preferences package.

To manage the migration use this package https://pub.dev/packages/version_migration/

Installation

Add in pubspec:

native_shared_preferences: ^2.0.10

Usage

To use this plugin, add native_shared_preferences in your pubspec.yaml file

Example
import 'package:flutter/material.dart';
import 'package:native_shared_preferences/native_shared_preferences.dart';

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      body: Center(
      child: RaisedButton(
        onPressed: _incrementCounter,
        child: Text('Increment Counter'),
        ),
      ),
    ),
  ));
}

_incrementCounter() async {
  NativeSharedPreferences prefs = await NativeSharedPreferences.getInstance();
  int counter = (prefs.getInt('counter') ?? 0) + 1;
  print('Pressed $counter times.');
  await prefs.setInt('counter', counter);
}
Testing

You can populate NativeSharedPreferences with initial values in your tests by running this code:

NativeSharedPreferences.setMockInitialValues (Map<String, dynamic> values);

所属分类

依赖

依赖清单 9 项

  • cupertino_icons^1.0.6
  • file^7.0.0
  • meta^1.9.1
  • shared_preferences^2.2.2
  • shared_preferences_linux^2.3.2
  • shared_preferences_macos^2.0.5
  • shared_preferences_platform_interface^2.3.1
  • shared_preferences_web^2.2.1
  • shared_preferences_windows^2.3.2

源码仓库

使用此插件的项目