Trending Articles       10 Flutter Listview Examples       Flutter Custom Button Example       Flutter Audio Recording       Flutter 2.0 Autocomplete TextField       Current Location Google Maps

Flutter GetX Storage | SharedPreferences

In this post we will learn about Getx Storage in Flutter application

What is Get_Storage Package in flutter?

Flutter Get_storage packages is a light weight package. Which will stora the data in the form of key value pair

It will store the data in app memory.

 

Flutter Sahrepreference with Getx Storage

 

The GetX Storage is built completly built using dart programming language & we can easily integrate with GETX framework of flutter

 

With get_storage we can store the data with the type of

  • String
  • int
  • double
  • map
  • list

 

 

So let's get started with get_storage example

 

Step 1: Create a flutter application

Step 2: Add required get_storage plugins in pubspec.yaml file

dependencies:
  flutter:
    sdk: flutter
  get_storage: ^1.3.2
  get:

 

Step 3: Import get_storage packages in our dart file by

import 'package:get_storage/get_storage.dart';

 

Step 4: Use Get Storage packages

Initialize get_storage

To Initialize storage, we must call GetStorage.init() method before loading the app

void main() async {
  await GetStorage.init();
  runApp(MyApp());
}

 

Create Instance of Storage object

final datacount = GetStorage();

 

Read and write data with get_storage

To write the data into get_storage object by below code

datacount.write("count", _count);

 

The write() is the method which will use to write data into storage

 

To read the data from object by

datacount.read('count')

 

read() is the method which we will use to read the data from the memory. To read the data we need to pass the key of the stored data.

 

How to remove the data from get_storage object

The get_storage contains a method remove, to remove the data from the storage memory

datacount.remove('count')

 

 

The GetX Storage example 1

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(
      appBar: AppBar(title: Text("GetX Storage"),
      backgroundColor: Colors.pink,),
      body: SafeArea(
        child: Center(
          child: Container(
            child: Text("${datacount.read('count')}",style: TextStyle(fontSize: 35),),
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: (){
          setState(() {
            _count++;
            datacount.write("count", _count);
          });
        },
        child: Icon(Icons.add),
      ),
    );
  }
}

 

 

 


648 Views

Author: RRTutors

Flutter Questions

Android Questions