local_value 1.2.0 copy "local_value: ^1.2.0" to clipboard
local_value: ^1.2.0 copied to clipboard

A simple abstraction to persist data in the local file system.

example/lib/main.dart

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

class CounterObj {
  int value;

  CounterObj(this.value);
}

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  final counterStorage = LocalSingleton<int>(id: 'counter');

  MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Local Data Manager Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      floatingActionButton: Builder(
          builder: (context) => Row(
                mainAxisSize: MainAxisSize.max,
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  FloatingActionButton(
                    onPressed: () => widget.counterStorage.read().then((value) {
                      if (value == null) {
                        ScaffoldMessenger.of(context).showSnackBar(
                            const SnackBar(
                                content: Text('no local data saved')));
                        return;
                      }

                      setState(() {
                        _counter = value;
                      });
                    }),
                    backgroundColor: Colors.orange,
                    child: const Icon(Icons.replay),
                  ),
                  FloatingActionButton(
                    onPressed: () =>
                        widget.counterStorage.write(_counter).then((_) {
                      ScaffoldMessenger.of(context).showSnackBar(
                          const SnackBar(content: Text('saved!')));
                    }),
                    backgroundColor: Colors.green,
                    child: const Icon(Icons.save),
                  ),
                  FloatingActionButton(
                    onPressed: _incrementCounter,
                    child: const Icon(Icons.add),
                  ),
                ],
              )),
    );
  }
}
8
likes
120
pub points
62%
popularity

Publisher

unverified uploader

A simple abstraction to persist data in the local file system.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter, flutter_secure_storage, path_provider, shared_preferences

More

Packages that depend on local_value