Advertisement
yudiwibisono

shared_pref1

Jun 3rd, 2023
1,241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.29 KB | None | 0 0
  1. //dependencies:
  2. //  shared_preferences: ^2.1.1
  3.  
  4. import 'package:flutter/material.dart';
  5. import 'package:shared_preferences/shared_preferences.dart';
  6.  
  7. void main() {
  8.   runApp(const MyApp());
  9. }
  10.  
  11. class MyApp extends StatefulWidget {
  12.   const MyApp({Key? key}) : super(key: key);
  13.  
  14.   @override
  15.   State<MyApp> createState() => _MyAppState();
  16. }
  17.  
  18. class _MyAppState extends State<MyApp> {
  19.   late Future<String> userId;
  20.  
  21.   //ambil dari sharedpref
  22.   //return userId
  23.   Future<String> ambilDataUser() async {
  24.     final prefs = await SharedPreferences.getInstance();
  25.     return (prefs.getString('userId') ?? "");
  26.   }
  27.  
  28.   //simpan ke sharepref
  29.   Future<void> simpanDataUser() async {
  30.     final prefs = await SharedPreferences.getInstance();
  31.     await prefs.setString('userId', "budiWati");
  32.   }
  33.  
  34.   //hapus data sharedpref
  35.   Future<void> hapusDataUser() async {
  36.     final prefs = await SharedPreferences.getInstance();
  37.     await prefs.remove('userId');
  38.   }
  39.  
  40.   @override
  41.   void initState() {
  42.     super.initState();
  43.     userId = ambilDataUser(); //isi userid
  44.   }
  45.  
  46.   @override
  47.   Widget build(BuildContext context) {
  48.     return MaterialApp(
  49.       home: Scaffold(
  50.         body: Center(
  51.             child: FutureBuilder<String>(
  52.                 future: userId,
  53.                 builder: (context, snapshot) {
  54.                   if (snapshot.hasData) {
  55.                     //user id belum ada di sharedpref
  56.                     if (snapshot.data == "") {
  57.                       return (Column(
  58.                           mainAxisAlignment: MainAxisAlignment.center,
  59.                           children: [
  60.                             Text("user belum login"),
  61.                             ElevatedButton(
  62.                               onPressed: () {
  63.                                 setState(() {
  64.                                   //set userid (simulasi)
  65.                                   //simpan ke sharedpref
  66.                                   //sekaligus refresh
  67.                                   simpanDataUser();
  68.                                   userId = ambilDataUser();
  69.                                 }); //refresh
  70.                               },
  71.                               child: const Text('Login'),
  72.                             ),
  73.                           ]));
  74.                     } else {
  75.                       //sudah ada datauser di sharedpref
  76.                       return (Column(
  77.                           mainAxisAlignment: MainAxisAlignment.center,
  78.                           children: [
  79.                             Text("userid: ${snapshot.data!}"),
  80.                             ElevatedButton(
  81.                               onPressed: () {
  82.                                 setState(() {
  83.                                   //hapus sharedpref
  84.                                   //sekaligus refresh
  85.                                   hapusDataUser();
  86.                                   userId = ambilDataUser();
  87.                                 }); //refresh
  88.                               },
  89.                               child: const Text('Logout'),
  90.                             ),
  91.                           ]));
  92.                     }
  93.                   } else {
  94.                     return const CircularProgressIndicator();
  95.                   }
  96.                 })),
  97.       ),
  98.     );
  99.   }
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement