#flutter
Storing Data Locally
Learn how to store data locally using SQLite and SharedPreferences. This section covers saving user data, app settings, and other persistent data.
Using SQLite for Persistent Storage
Add the sqflite package to your pubspec.yaml:
1dependencies:2sqflite: ^2.0.0
Saving Data with SQLite
1import 'package:flutter/material.dart';2import 'package:sqflite/sqflite.dart';3import 'package:path/path.dart';45void main() {6runApp(SQLiteApp());7}89// SQLite demo for saving data locally10class SQLiteApp extends StatefulWidget {11@override12_SQLiteAppState createState() => _SQLiteAppState();13}1415class _SQLiteAppState extends State<SQLiteApp> {16Database _database;1718@override19void initState() {20super.initState();21_initializeDB();22}2324// Initializing the SQLite database25Future<void> _initializeDB() async {26_database = await openDatabase(27join(await getDatabasesPath(), 'my_database.db'),28onCreate: (db, version) {29return db.execute(30"CREATE TABLE items(id INTEGER PRIMARY KEY, name TEXT)",31);32},33version: 1,34);35}3637// Inserting a new item into the database38Future<void> _insertItem(String name) async {39await _database.insert(40'items',41{'name': name},42conflictAlgorithm: ConflictAlgorithm.replace,43);44}4546@override47Widget build(BuildContext context) {48return MaterialApp(49home: Scaffold(50appBar: AppBar(title: Text('SQLite Example')),51body: Center(52child: ElevatedButton(53onPressed: () => _insertItem('Test Item'),54child: Text('Insert Data'),55),56),57),58);59}60}
Using SharedPreferences for Simple Key-Value Storage
Add the shared_preferences package to your pubspec.yaml:
1dependencies:2shared_preferences: ^2.0.6
Using SharedPreferences
1import 'package:flutter/material.dart';2import 'package:shared_preferences/shared_preferences.dart';34void main() {5runApp(SharedPreferencesApp());6}78// Storing key-value pairs with SharedPreferences9class SharedPreferencesApp extends StatefulWidget {10@override11_SharedPreferencesAppState createState() => _SharedPreferencesAppState();12}1314class _SharedPreferencesAppState extends State<SharedPreferencesApp> {15String _storedValue = 'No Value';1617// Method to store data18Future<void> _storeValue() async {19final prefs = await SharedPreferences.getInstance();20await prefs.setString('stored_value', 'Hello Flutter');21}2223// Method to retrieve stored data24Future<void> _retrieveValue() async {25final prefs = await SharedPreferences.getInstance();26setState(() {27_storedValue = prefs.getString('stored_value') ?? 'No Value';28});29}3031@override32Widget build(BuildContext context) {33return MaterialApp(34home: Scaffold(35appBar: AppBar(title: Text('SharedPreferences Example')),36body: Center(37child: Column(38mainAxisAlignment: MainAxisAlignment.center,39children: [40Text(_storedValue),41ElevatedButton(42onPressed: _storeValue,43child: Text('Store Value'),44),45ElevatedButton(46onPressed: _retrieveValue,47child: Text('Retrieve Value'),48),49],50),51),52),53);54}55}
Key Takeaways
- Use SQLite for complex persistent storage like storing user data.
- Use SharedPreferences for simple key-value storage like user preferences.
©2024 Codeblockz
Privacy Policy