Pemrograman Mobile
Muhamad Saad Nurul Ishlah, M.Comp.
Dept. Sistem Informasi & Dept. Ilmu Komputer, Universitas Pakuan
Empat cara menyimpan data
Penyimpanan data sederhana/kecil. Implementasi mudah
Bentuk penyimpanan berupa pasangan key-value
Android: SharedPreferences
iOS/macOS: NSUserDefaults
Web: localStorage
Windows: AppData
Linux: XDG_DATA_HOME
Fungsi penggunaan:
Data settings, Bookmarks, dll
Kekurangan
Data hilang ketika aplikasi dihapus
Bukan bawaan dari Flutter – library/plugin shared_preferences
Tipe data yang didukung
int,
String,
double
bool
List<String>
Tambah dependency pada pubspec.yaml
Import package shared_preferences pada kode
Gunakan pada kode
Menyimpan data
Mengembalikan data
Menghapus data
dependencies:
flutter:
sdk: flutter
shared_preferences: "<versi terbaru>"
Tambahkah dependency shared_preferences di pubspec.yaml
import 'package:shared_preferences/shared_preferences.dart';
Import paket shared_preferences
addStringToSP() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('stringValue', "abc");
}
Buat instans dari kelas SharedPreferences untuk menggunakan SharedPreferences
// Menyimpan nilai String
addStringToSP() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('stringValue', "abc");
}
// Menyimpan nilai Integer
addIntToSP() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setInt('stringValue', 110);
}
// Menyimpan nilai Double
addDoubleToSP() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setDouble('doubleValue', 115.0);
}
// Menyimpan nilai Boolean
addBoolToSP() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool('boolValue', true);
}
// Menyimpan nilai List dari String
addBoolToSP() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setStringList('items', <String>['Earth', 'Moon', 'Sun']);
}
Untuk menyimpan Data
Future<String> getStringValuesSF() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
//Return String
String stringValue = prefs.getString('stringValue');
return stringValue;
}
Future<bool> getBoolValuesSF() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
//Return bool
bool boolValue = prefs.getBool('boolValue');
return boolValue;
}
Future<int> getIntValuesSF() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
//Return int
int intValue = prefs.getInt('intValue');
return intValue;
}
Future<double> getDoubleValuesSF() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
//Return double
double doubleValue = prefs.getDouble('doubleValue');
return doubleValue;
}
Future<List<String>> getListOfString() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
// Try reading data from the 'items' key. If it doesn't exist, returns null.
final List<String>? items = prefs.getStringList('items');
return items;
}
// Jika nilai tidak terdapat di pref, bisa tambahkan nilai default
int intValue= await prefs.getInt('intValue') ?? 0;
Untuk membaca Data
removeValues() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
//Remove String
prefs.remove("stringValue");
//Remove bool
prefs.remove("boolValue");
//Remove int
prefs.remove("intValue");
//Remove double
prefs.remove("doubleValue");
}
Untuk menghapus Data
SharedPreferences prefs = await SharedPreferences.getInstance();
bool CheckValue = prefs.containsKey('value');
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of the application.
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Shared preferences demo',
home: MyHomePage(title: 'Shared preferences demo'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
@override
void initState() {
super.initState();
_loadCounter();
}
//Loading counter value on start
Future<void> _loadCounter() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
_counter = (prefs.getInt('counter') ?? 0);
});
}
//Incrementing counter after click
Future<void> _incrementCounter() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
_counter = (prefs.getInt('counter') ?? 0) + 1;
prefs.setInt('counter', _counter);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}