Icon
Get In Touch
#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:

1
dependencies:
2
sqflite: ^2.0.0

Saving Data with SQLite

1
import 'package:flutter/material.dart';
2
import 'package:sqflite/sqflite.dart';
3
import 'package:path/path.dart';
4
5
void main() {
6
runApp(SQLiteApp());
7
}
8
9
// SQLite demo for saving data locally
10
class SQLiteApp extends StatefulWidget {
11
@override
12
_SQLiteAppState createState() => _SQLiteAppState();
13
}
14
15
class _SQLiteAppState extends State<SQLiteApp> {
16
Database _database;
17
18
@override
19
void initState() {
20
super.initState();
21
_initializeDB();
22
}
23
24
// Initializing the SQLite database
25
Future<void> _initializeDB() async {
26
_database = await openDatabase(
27
join(await getDatabasesPath(), 'my_database.db'),
28
onCreate: (db, version) {
29
return db.execute(
30
"CREATE TABLE items(id INTEGER PRIMARY KEY, name TEXT)",
31
);
32
},
33
version: 1,
34
);
35
}
36
37
// Inserting a new item into the database
38
Future<void> _insertItem(String name) async {
39
await _database.insert(
40
'items',
41
{'name': name},
42
conflictAlgorithm: ConflictAlgorithm.replace,
43
);
44
}
45
46
@override
47
Widget build(BuildContext context) {
48
return MaterialApp(
49
home: Scaffold(
50
appBar: AppBar(title: Text('SQLite Example')),
51
body: Center(
52
child: ElevatedButton(
53
onPressed: () => _insertItem('Test Item'),
54
child: 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:

1
dependencies:
2
shared_preferences: ^2.0.6

Using SharedPreferences

1
import 'package:flutter/material.dart';
2
import 'package:shared_preferences/shared_preferences.dart';
3
4
void main() {
5
runApp(SharedPreferencesApp());
6
}
7
8
// Storing key-value pairs with SharedPreferences
9
class SharedPreferencesApp extends StatefulWidget {
10
@override
11
_SharedPreferencesAppState createState() => _SharedPreferencesAppState();
12
}
13
14
class _SharedPreferencesAppState extends State<SharedPreferencesApp> {
15
String _storedValue = 'No Value';
16
17
// Method to store data
18
Future<void> _storeValue() async {
19
final prefs = await SharedPreferences.getInstance();
20
await prefs.setString('stored_value', 'Hello Flutter');
21
}
22
23
// Method to retrieve stored data
24
Future<void> _retrieveValue() async {
25
final prefs = await SharedPreferences.getInstance();
26
setState(() {
27
_storedValue = prefs.getString('stored_value') ?? 'No Value';
28
});
29
}
30
31
@override
32
Widget build(BuildContext context) {
33
return MaterialApp(
34
home: Scaffold(
35
appBar: AppBar(title: Text('SharedPreferences Example')),
36
body: Center(
37
child: Column(
38
mainAxisAlignment: MainAxisAlignment.center,
39
children: [
40
Text(_storedValue),
41
ElevatedButton(
42
onPressed: _storeValue,
43
child: Text('Store Value'),
44
),
45
ElevatedButton(
46
onPressed: _retrieveValue,
47
child: 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