Get Storage in Flutter is a very fast, extra lightweight & synchronous key-value pair package that will help you to store app data in memory. It is written entirely in Dart and easily integrates with the Get framework of Flutter. Generally, in Flutter, we use Shared preference to store app data. It is an alternative to app-shared preference.
For more, please check out our Flutter app development page.
Feature of Get storage:
GetStorage is not fast, it is absurdly fast for being memory-based.
All of its operations are instantaneous.
A backup of each operation is placed in a Container on the disk. Each container has its file.
We have multiple benefits of using Get Storage in Flutter. Now, let’s check how can we implement Get Storage in Flutter.
Step 1: Create a flutter project.
Step 2: Add dependency in your pubspec file.
Step 3: Initialize Get Storage.
To initialize get storage, we need to call GetStorage().init in the main function.
1 2 3 4 |
void main() async{ await GetStorage.init(); runApp(const MyApp()); } |
Step 4: How to use the Get storage package.
Create an Instance of the Storage object.
1 |
final box = GetStorage(); |
Read and write data using Get storage.
1 2 3 4 5 |
//write data box.write('finalCount', _counter); // read data box.read('finalCount') |
We write data in key-pair value using the write function and we read data using the read function with the help of key.
Get storage has multiple functions for multiple uses. Let’s check other functions.
Remove the data from get_storage object.
1 |
box.remove('finalCount') |
Listen to changes.
1 |
box.listen(() => print('box changed')); |
Stop listening to changes.
1 |
box.removeListen(listen); |
Erase your container
1 |
box.erase(); |
Get Storage Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
import 'package:flutter/material.dart'; import 'package:get_storage/get_storage.dart'; void main() async{ await GetStorage.init(); 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: const MyHomePage(), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({Key? key}) : super(key: key); @override State<MyHomePage> createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { int _counter = 0; final box = GetStorage(); void _incrementCounter() { setState(() { _counter++; box.write('finalCount', _counter); }); } @override Widget build(BuildContext context) { box.writeIfNull('finalCount', 0); return Scaffold( appBar: AppBar( title: const Text("Get storage demo"), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ const Text( 'You have pushed the button this many times:', ), Text( '${box.read('finalCount')}', style: Theme.of(context).textTheme.headline4, ), ], ), ), floatingActionButton: FloatingActionButton( onPressed: _incrementCounter, tooltip: 'Increment', child: const Icon(Icons.add), ), // This trailing comma makes auto-formatting nicer for build methods. ); } } |
Output:
Thanks for reading this article.
If I got something wrong, let me know in the comments. I would love to improve.
For more interesting blogs check out here – https://mobikul.com/blog/
Reference link: https://pub.dev/packages/get_storage