搜尋套件、儲存庫與 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

原始碼儲存庫

使用此套件的專案