mayr_storage 0.1.1
mayr_storage: ^0.1.1 copied to clipboard
Simple and unified storage solution for Flutter apps. Easily manage SharedPreferences, EncryptedSharedPreferences, and GetStorage with a clean, consistent API.
πΌ Mayr Flutter Storage Util #
A lightweight Flutter package for seamless access to SharedPreferences, SecureStorage, and GetStorage using a simple and expressive API.
One storage interface. Multiple backends. SharedPreferences, Secure Storage, and GetStorage made effortless for Flutter apps.
β¨ Features #
-
Easy Key Definition
Define your storage keys once and use them throughout your app β clean, organised, and type-safe.
-
Unified API Across Storage Types
Whether itβs SharedPreferences, EncryptedSharedPreferences, or GetStorage, the syntax stays the same. One style, multiple backends.
-
Zero Boilerplate
No need to manually set up storage instances or repeat logic. Just define and use.
-
Simple Read, Write, and Delete
read()
to fetch a valuewrite(value)
to save a valuedelete()
to remove a value
-
Support for Multiple Data Types
Supports all basic types like String, int, double, bool, List
-
Clean, Scalable, and Developer-Friendly
Perfect for growing codebases where storage management needs to stay intuitive and reliable.
π Getting started #
-
Add
mayr_storage
to yourpubspec.yaml
:dependencies: mayr_storage: # check for the latest version on pub.dev
-
Instal the package:
flutter pub get
-
Import it into your Dart file:
import 'package:mayr_storage/mayr_storage.dart';
Alternatively, you could install it using the command
flutter pub add mayr_storage
βοΈ Important: Initialise Storage First #
Before using any of the storage features, you must initialise the storage engine.
Do this at the start of your app, typically inside the main()
function:
import 'package:flutter/material.dart';
import 'package:mayr_storage/mayr_storage.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await MayrStorage.init();
runApp(const MyApp());
}
π Note: Failing to call
MayrStorage.init()
before accessing any storage key will cause unexpected errors.
π οΈ Usage #
Define your storage keys once in a dedicated Storage
class: #
import 'package:mayr_storage/mayr_storage.dart';
class Storage {
static final userToken = 'SK_USER_TOKEN'.storage<String>();
static final username = 'SK_USERNAME'.secureStorage<String>();
static final userAge = 'SK_USER_AGE'.boxStorage<int>();
}
PS: You can name the class whatever you want, you are not required to name it Storage
βοΈ Write to Storage #
await Storage.userToken.write('abc123');
await Storage.username.write('YoungMayor');
Storage.userAge.write(25); // Box Storage is synchronous so no need for waiting
π Read from Storage #
String? token = await Storage.userToken.read();
String? name = await Storage.username.read();
int? age = Storage.userAge.read();
ποΈ Delete from Storage #
await Storage.userToken.delete();
await Storage.username.delete();
Storage.userAge.delete();
π Choosing the Right Storage #
π οΈ .storage #
-
This uses SharedPreferences internally.
-
It is simple key-value storage, ideal for lightweight, non-sensitive data like user settings, theme preferences, onboarding completion flags, etc.
-
Not encrypted, so not suitable for secrets (e.g. tokens, passwords).
πΉ Example Use Cases:
Saving theme mode (dark/light), saving user selected language.
π‘οΈ .secureStorage #
-
This uses EncryptSharedPreferences internally.
-
Data is encrypted, making it safe for sensitive information like tokens, credentials, secure flags.
-
Better security compared to normal
.storage
.
πΉ Example Use Cases:
Saving login tokens, saving sensitive user settings, saving payment authentication preferences.
π¦ .boxStorage #
-
This uses GetStorage internally.
-
It's a very fast and lightweight local storage.
-
Great for caching larger amounts of data or building offline-first features.
-
It's not encrypted but super efficient.
-
It is synchronous, goodbye to waiting
πΉ Example Use Cases:
Storing offline data, caching lists, storing app states, drafts, local session data.
Type | Backend Used | Encryption | Synchronous | Best for |
---|---|---|---|---|
.storage | SharedPreferences | β | β | Light, non-sensitive key-value data |
.secureStorage | EncryptSharedPreferences | β | β | Sensitive data like tokens |
.boxStorage | GetStorage | β | β | Offline data caching, larger datasets |
β Best Practices #
-
Always initialise MayrStorage early
Ensure you call
await MayrStorage.init();
before using any storage key. Preferably inside yourmain()
function before running the app. -
Group your keys properly
Define all your keys inside a dedicated class (or multiple classes if needed). This improves maintainability and readability.
class Storage { static final isFirstLaunch = 'SK_FIRST_LAUNCH'.storage<bool>(); static final authToken = 'SK_AUTH_TOKEN'.secureStorage<String>(); static final cachedPosts = 'SK_CACHED_POSTS'.boxStorage<List<String>>(); }
-
Use the correct storage type for the data
- Use
.storage()
for simple, non-sensitive values (e.g. booleans, settings). - Use
.secureStorage()
for sensitive or confidential data (e.g. tokens, passwords). - Use
.boxStorage()
for caching and data sets.
- Use
-
Use meaningful key names
Use key names that clearly describe their purpose. For example, prefer
'SK_USER_THEME_MODE'
over'SK_TM'
. -
Encrypt highly sensitive data yourself if needed
.secureStorage() provides basic encryption. If you're dealing with extremely sensitive data, consider encrypting it manually before saving.
-
Clean up after yourself
If a key is no longer needed (e.g. on logout), make sure to delete it to avoid stale data.
await Storage.authToken.delete();
-
Do not overuse local storage
Storage is great, but avoid putting huge amounts of unnecessary data into local storage. It can slow down your app over time.
π’ Additional Information #
π€ Contributing #
Contributions are highly welcome! If you have ideas for new extensions, improvements, or fixes, feel free to fork the repository and submit a pull request.
Please make sure to:
- Follow the existing coding style.
- Write tests for new features.
- Update documentation if necessary.
Let's build something amazing together!
π Reporting Issues #
If you encounter a bug, unexpected behaviour, or have feature requests:
- Open an issue on the repository.
- Provide a clear description and steps to reproduce (if it's a bug).
- Suggest improvements if you have any ideas.
Your feedback helps make the package better for everyone!
π Licence #
This package is licensed under the MIT License β which means you are free to use it for commercial and non-commercial projects, with proper attribution.
See the LICENSE file for more details.
π Support #
If you find this package helpful, please consider giving it a βοΈ on GitHub β it motivates and helps the project grow!
You can also support by:
- Sharing the package with your friends, colleagues, and tech communities.
- Using it in your projects and giving feedback.
- Contributing new ideas, features, or improvements.
Every little bit of support counts! ππ