hyper_storage_flutter

A package that makes it easy to use hyper_storage in Flutter applications.

Full Documentation

Features

  • ValueListenable Support: Listen to changes in your storage using ValueListenable.
  • Reactive UI: Automatically rebuild your widgets when the data in the storage changes.
  • Seamless Integration: Integrates smoothly with the Flutter framework and hyper_storage.

Getting started

Add hyper_storage_flutter to your pubspec.yaml dependencies:

dependencies:
  flutter:
    sdk: flutter
  hyper_storage: ^0.1.0 # Replace with the latest version
  hyper_storage_flutter: ^0.1.0 # Replace with the latest version

Then, run flutter pub get.

Usage

Hyper Storage Flutter Examples

This file provides examples of how to use the hyper_storage_flutter package to build reactive user interfaces in Flutter.

Listening to a Single Key

There are multiple ways to listen to a single key in your storage.

  • Using ItemHolder: ItemHolder is listenable and can be streamed too.
  • Using ItemHolder.asValueNotifier: This is a convenient way to get a ValueNotifier for a specific key.
  • Using storage.stream<E>(key): This provides a stream of changes for a specific key.

You can use asValueNotifier on an ItemHolder to obtain a ValueNotifier for a specific key. This allows you to rebuild your UI automatically whenever the value of that key changes.

Using ItemHolder as ValueNotifier.

Note: ItemHolder.asValueNotifier must be called outside of the build method, typically in initState as it will create a new ValueNotifier each time it is called.

import 'package:flutter/material.dart';
import 'package:hyper_storage/hyper_storage.dart';
import 'package:hyper_storage_flutter/hyper_storage_flutter.dart';

class CounterScreen extends StatefulWidget {
  final HyperStorage storage;

  const CounterScreen({super.key, required this.storage});

  @override
  State<CounterScreen> createState() => _CounterScreenState();
}

class _CounterScreenState extends State<CounterScreen> {
  late final ItemHolder<int> _counterHolder;
  late final ValueNotifier<int?> _counterNotifier;

  @override
  void initState() {
    super.initState();
    _counterHolder = widget.storage.itemHolder<int>('counter');
    _counterNotifier = _counterHolder.asValueNotifier();
  }

  @override
  void dispose() {
    _counterNotifier.dispose();
    _counterHolder.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ValueListenableBuilder<int?>(
          valueListenable: _counterNotifier,
          builder: (context, counter, child) {
            return Text('Counter: ${counter ?? 0}');
          },
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          final currentCounter = await _counterHolder.get() ?? 0;
          await _counterHolder.set(currentCounter + 1);
        },
        child: const Icon(Icons.add),
      ),
    );
  }
}

Using ItemHolder with StreamBuilder.

Note: It is safe to reuse the same ItemHolder instance multiple times without disposing it, as it manages its own resources. Calling itemHolder multiple times with the same key will return the same instance.

import 'package:flutter/material.dart';
import 'package:hyper_storage/hyper_storage.dart';
import 'package:hyper_storage_flutter/hyper_storage_flutter.dart';

class CounterScreen extends StatefulWidget {
  final HyperStorage storage;

  const CounterScreen({super.key, required this.storage});

  @override
  State<CounterScreen> createState() => _CounterScreenState();
}

class _CounterScreenState extends State<CounterScreen> {
  late final ItemHolder<int> _counterHolder = widget.storage.itemHolder<int>('counter');

  @override
  void dispose() {
    _counterHolder.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: StreamBuilder<int?>(
          stream: _counterHolder,
          builder: (context, snapshot) {
            return Text('Counter: ${snapshot.data ?? 0}');
          },
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          final currentCounter = await _counterHolder.get() ?? 0;
          await _counterHolder.set(currentCounter + 1);
        },
        child: const Icon(Icons.add),
      ),
    );
  }
}

Using streams with StreamBuilder.

Note: storage.stream<E>(key) is safe to call inside the build method as it manages the stream internally.

import 'package:flutter/material.dart';
import 'package:hyper_storage/hyper_storage.dart';
import 'package:hyper_storage_flutter/hyper_storage_flutter.dart';

class CounterScreen extends StatefulWidget {
  final HyperStorage storage;

  const CounterScreen({super.key, required this.storage});

  @override
  State<CounterScreen> createState() => _CounterScreenState();
}

class _CounterScreenState extends State<CounterScreen> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: StreamBuilder<int?>(
          stream: widget.storage.stream<int>('counter'),
          builder: (context, snapshot) {
            return Text('Counter: ${snapshot.data ?? 0}');
          },
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          final currentCounter = await widget.storage.getInt('counter') ?? 0;
          await widget.storage.setInt('counter', currentCounter + 1);
        },
        child: const Icon(Icons.add),
      ),
    );
  }
}

Contributing

Contributions are welcome! Please feel free to open an issue or submit a pull request.

License

BSD 3-Clause License

Copyright (c) 2025, Hyperdesigned

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.