Hi Guys, Welcome to Proto Coders Point, In this Flutter Article tutorial we will learn about GetX Get_Storage package
What is Get_Storage Package in flutter?
In Flutter get storage is a very fast, extra light weight & synchronous key-value pair package that will help you to store app data in memory such as, if user is signed in, if yes, then all the details about the user.
The GetX Storage is built completly built using dart programming language & we can easily integrate with GETX framework of flutter.
This Flutter package to store user data is fully tested & supported on most of the famous operating system like, Android, iOS, Web, Windows & much more OS.
Which kind of data get_storage can handle/store?
Using Getx Storage we can store data type such as String, int, double, Map, and a list of values/arrays.
How to install flutter package library – Get Storage library
Official site : https://pub.dev/packages/get_storage/install
- Add the dependencies in pubspec.yaml file:
In your flutter project structure you will find a file by name “pubspec.yaml” file,
Open it & under dependencies section add getx and get_storage packagedependencies: get_storage: ^1.3.2 get:
- Import them
After adding the required dependencies, you need to import it where you want to add getx storage feature.
import 'package:get_storage/get_storage.dart';
How to use getX Get Storage package?
- Initialization
To Initialize storage, you must call GetStorage.init() method before loading the app / before calling runApp
void main() async { await GetStorage.init(); //get storage initialization runApp(MyApp()); }
Here we need to make the main as “async” & to initialize GetStorage we must use “await”.
So that if any data is been stored in device, it get loaded before the actual app - Create a Instance of get Storage class
final datacount = GetStorage(); // instance of getStorage class
- How to store/write data in getx storage
To store/write data or information you must make use of “write“.
Example:datacount.write("count", _count);
Here count is key and _count is a value.
- How to read value from key
To read values you need to use “read”
Example:datacount.read('count')
- To delete/remove the data
you can delete/remove the data that is been stored by making use of “remove”
Example:datacount.remove('count');
So now we are down with basic of get storage package, Now let’s Check with a example
Example 1 : Counter App – Flutter sharedpreferences alternative
In this example, we will add Get Storage feature to default code if flutter i.e Increment valuable on button press.
Here will be store the counter variable to get Storage instance class, so that when the app is removed in future it remember the last added counter value.
main.dart
import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:get_storage/get_storage.dart'; void main() async { await GetStorage.init(); runApp(MyApp()); } class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: HomePage() ); } } class HomePage extends StatefulWidget { @override _HomePageState createState() => _HomePageState(); } class _HomePageState extends State<HomePage> { int _count = 0; final datacount = GetStorage(); @override void initState() { // TODO: implement initState super.initState(); if(datacount.read('count')!= null) { _count = datacount.read('count'); } } @override Widget build(BuildContext context) { datacount.writeIfNull("count", 0); return Scaffold( body: SafeArea( child: Center( child: Container( child: Text("${datacount.read('count')}"), ), ), ), floatingActionButton: FloatingActionButton( onPressed: (){ setState(() { _count++; datacount.write("count", _count); }); }, child: Icon(Icons.add), ), ); } }