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

插件详情v3.1.0

cross_local_storage

适用于移动设备(Android/iOS)、Web 以及桌面端(Windows、macOS、Linux)的 SharedPreferences 封装。同时还提供 JSON 文件存储功能。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
343
喜欢数
32
Pub 分数
160

安装插件

flutter pub add cross_local_storage

插件介绍

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

CrossLocalStorage 是一个 Flutter/Dart 包,为移动(Android/iOS)、Web 和桌面平台(Windows、macOS、Linux)提供统一的持久化存储接口。它在移动和 Web 平台上封装了 SharedPreferences,而在桌面环境中则提供基于 JSON 文件的存储。该包确保了跨平台的数据访问一致性,简化了跨平台应用开发。它支持键值存储和结构化 JSON 文件持久化,非常适合可靠地存储用户偏好、应用状态或小型数据集。

适合场景

  • 跨平台存储用户偏好
  • 在多平台应用中持久化应用状态
  • 将配置文件保存为 JSON
  • 在网页和桌面应用程序中管理本地数据
  • 在移动设备和桌面设备之间共享简单数据
  • 启用离线优先功能

主要特点

  • 统一 API 适用于所有平台
  • 桌面端的JSON文件存储
  • SharedPreferences 在移动/web 上的回退
  • 平台无关的数据访问
  • 支持字符串、整数、布尔值和复杂类型
  • 自动序列化/反序列化

适合对象

  • 跨平台 Flutter 开发人员
  • 需要在多个目标上进行本地持久化的应用
  • 开发人员避免使用平台特定代码
  • 使用 JSON 存储本地数据的项目
  • 优先考虑代码重用的团队
  • 需要轻量级本地存储的应用

使用建议

  • 不适用于大规模数据
  • JSON 文件存储可能会影响桌面端的性能
  • 限于简单数据结构
  • 无内置加密
  • 在桌面端需要仔细处理文件路径
  • 不适用于高频写入

原始 README

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

cross_local_storage

Build Pub GitHub GitHub stars

Wraps NSUserDefaults (on iOS and macOS), SharedPreferences (on Android), LocalStorage (on Web) and JSON file (on Windows and Linux), providing a persistent store for simple data. Data is persisted to disk asynchronously. Neither platform can guarantee that writes will be persisted to disk after returning and this plugin must not be used for storing critical data.

Getting Started

In order to use this plugin, add dependency in the pubspec.yaml:

cross_local_storage: any

or

cross_local_storage:
  git:
    url: https://github.com/marchdev-tk/cross_local_storage

Add an import to dart file:

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

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

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

Feature requests and Bug reports

Feel free to post a feature requests or report a bug here.

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目

暂无相关数据。