cross_local_storage 1.1.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 88

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: ^1.1.0

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.

Changelog #

[1.1.1] #

  • Updated readme

[1.1.0] #

  • Renamed plugin to cross_local_storage

[1.0.1] #

  • Fixed issue when opening empty file

[1.0.0] #

  • Created common interface for wrapper of SharedPreferences on both Mobile (Android/iOS), Web and Desktop (Windows, macOS, Linux)

example/lib/main.dart

// Copyright (c) 2020, the MarchDev Toolkit project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:flutter/material.dart';

import 'package:cross_local_storage/cross_local_storage.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Local Storage Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final _sharedPrefController = TextEditingController();

  LocalStorageInterface _localStorage;
  String _prefStatus = '';

  void _initLocalStorage() async {
    _localStorage = await LocalStorage.getInstance();
  }

  @override
  void initState() {
    super.initState();
    _initLocalStorage();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Local Storage Example'),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Center(
            child: ConstrainedBox(
              constraints: BoxConstraints(maxWidth: 300),
              child: TextField(
                controller: _sharedPrefController,
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  hintText: 'Type something to store...',
                ),
              ),
            ),
          ),
          Center(
            child: RaisedButton(
              onPressed: () async {
                final result = await _localStorage.setString(
                    'value', _sharedPrefController.text);
                setState(() => _prefStatus = result
                    ? 'Successfuly added to the Shared Prefs'
                    : 'Error occured while adding to the Shared Prefs');
              },
              child: Text('Add to shared prefs'),
            ),
          ),
          Center(
            child: RaisedButton(
              onPressed: () {
                final result = _localStorage.getString('value');
                setState(() =>
                    _prefStatus = 'Retreived value from Shared Prefs: $result');
              },
              child: Text('Get from shared prefs'),
            ),
          ),
          Center(
            child: RaisedButton(
              onPressed: () {
                _localStorage.clear();
                setState(() => _prefStatus = 'Cleared Shared Prefs');
              },
              child: Text('Clear shared prefs'),
            ),
          ),
          Center(child: Text(_prefStatus)),
        ],
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  cross_local_storage: ^1.1.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:cross_local_storage/cross_local_storage.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
76
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
88
Learn more about scoring.

We analyzed this package on May 26, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.1
  • pana: 0.13.8-dev
  • Flutter: 1.17.0

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
shared_preferences ^0.5.6+2 0.5.7+3
shared_preferences_macos ^0.0.1+6 0.0.1+9
shared_preferences_web ^0.1.2+4 0.1.2+7
Transitive dependencies
collection 1.14.12
flutter_web_plugins 0.0.0
meta 1.1.8
shared_preferences_platform_interface 1.0.4
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test
pedantic ^1.8.0