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

插件详情v6.0.0

localstorage

适用于 Flutter 的本地存储。React Native AsyncStorage 的替代方案。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1.3万
喜欢数
382
Pub 分数
160

安装插件

flutter pub add localstorage

插件介绍

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

Localstorage 是一个 Flutter 包,提供持久化的键值存储,作为 React Native 的 AsyncStorage 的替代方案。它可在 Android、iOS、Web、macOS、Windows 和 Linux 等多个平台上实现应用重启后的数据存储与读取。该包以简单易用为核心设计,支持异步读写字符串、数字以及可序列化为 JSON 的对象。该包抽象了平台特定的存储机制,确保在不同环境下的行为一致性。适用于缓存、用户偏好设置或无需完整数据库的小规模数据持久化场景。

适合场景

  • 存储用户偏好
  • 缓存 API 响应
  • 持久化应用状态
  • 保存表单数据
  • 管理身份验证令牌

主要特点

  • 跨平台兼容性
  • 异步读/写操作
  • JSON 序列化支持
  • 简单 API
  • 轻便且快速

适合对象

  • 小规模数据持久化
  • 离线优先的应用程序
  • 用户设置管理
  • Prototyping
  • 需要轻量级存储的应用

使用建议

  • 不适用于大型数据集
  • 无内置查询功能
  • 数据默认未加密
  • 仅限基本数据类型
  • 需要手动错误处理

原始 README

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

LocalStorage

LocalStorage for Flutter.

[!IMPORTANT]
LocalStorage is not intended to store large amounts or sensitive data.

Installation

flutter pub add localstorage

or add dependency to pubspec.yaml manually

dependencies:
  localstorage: ^5.0.0

API docs

LocalStorage API documentation

Usage

import 'package:flutter/material.dart';
import 'package:localstorage/localstorage.dart';

late final ValueNotifier<int> notifier;

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await initLocalStorage();

  notifier = ValueNotifier(int.parse(localStorage.getItem('counter') ?? '0'));
  notifier.addListener(() {
    localStorage.setItem('counter', notifier.value.toString());
  });

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: ValueListenableBuilder<int>(
            valueListenable: notifier,
            builder: (context, value, child) {
              return Text('Pressed $value times');
            },
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            notifier.value++;
          },
          child: const Icon(Icons.add),
        ),
      ),
    );
  }
}

Contributors

License

MIT

所属分类

依赖

依赖清单 3 项

源码仓库

使用此插件的项目