0
0
Fluttermobile~5 mins

Why local storage enables offline data in Flutter

Choose your learning style9 modes available
Introduction

Local storage saves data directly on your device. This lets your app work even without internet.

You want users to see their saved notes anytime, even offline.
Your app needs to remember user settings without asking the internet.
You want to load some data fast without waiting for a network.
Your app should keep working in places with poor or no signal.
Syntax
Flutter
final prefs = await SharedPreferences.getInstance();
await prefs.setString('key', 'value');
String? value = prefs.getString('key');
This example uses the SharedPreferences package for simple key-value storage.
You must await the instance before reading or writing data.
Examples
Store and read an integer value using SharedPreferences.
Flutter
final prefs = await SharedPreferences.getInstance();
await prefs.setInt('counter', 10);
int? count = prefs.getInt('counter');
Save and retrieve a boolean value to remember login state.
Flutter
final prefs = await SharedPreferences.getInstance();
await prefs.setBool('isLoggedIn', true);
bool? loggedIn = prefs.getBool('isLoggedIn');
Sample App

This app shows a counter that saves its value locally. You can close and open the app, and the number stays the same without internet.

Flutter
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  int _counter = 0;

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

  Future<void> _loadCounter() async {
    final prefs = await SharedPreferences.getInstance();
    setState(() {
      _counter = prefs.getInt('counter') ?? 0;
    });
  }

  Future<void> _incrementCounter() async {
    final prefs = await SharedPreferences.getInstance();
    setState(() {
      _counter++;
    });
    await prefs.setInt('counter', _counter);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Offline Counter')),
        body: Center(
          child: Text('You have pressed the button $_counter times.'),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: _incrementCounter,
          child: Icon(Icons.add),
          tooltip: 'Increment counter',
        ),
      ),
    );
  }
}
OutputSuccess
Important Notes

Local storage is fast and works without internet, but it is limited in size.

Use local storage for small data like settings, counters, or cached info.

For bigger or complex data, consider databases like SQLite.

Summary

Local storage keeps data on the device so apps can work offline.

SharedPreferences is a simple way to save small key-value data in Flutter.

Use local storage to improve user experience when internet is unavailable.