Hi Coders,
This is the 8th part of Getx Series and if you still didn't check other articles regarding this series then you can with Getx playlist and i already explained everything about the GetStorage in the video which is 👉
Let's learn something new today:
pubspec.yaml
get_storage: ^2.1.1
Our website also features a YouTube channel dedicated to coding and tech-related content. Additionally, we specialise in mobile application development. If you need assistance or have any inquiries, feel free to reach out—we’re just a message away!
Visit youtube 👉 CODEMICROS
main.dart
main() async {
await GetStorage.init();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'Flutter Demo',
locale: const Locale('en', 'US'),
translations: LocalString(),
theme: ThemeData.light(),
darkTheme: ThemeData.dark(),
// theme: ThemeData(
// colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
// useMaterial3: true,
// ),
initialRoute: AppPages.initial,
getPages: AppPages.routes,
);
}
}
get_storage.dart
import 'package:get_storage/get_storage.dart';
abstract class Storage {
static final GetStorage _storage = GetStorage();
static GetStorage get storage => _storage;
// Save value
static Future<void> saveValue(String key, dynamic value) =>
_storage.writeIfNull(key, value);
//Get Value
static T? getValue<T>(String key) => _storage.read<T>(key);
// has data
static bool hasData(String key) => _storage.hasData(key);
//Remove value
static Future<void> removeValue(String key) => _storage.remove(key);
// Clear Storage
static Future<void> clearStorage() => _storage.erase();
}
save_storage_controller.dart
class SaveStorageController extends GetxController {
TextEditingController saveStorageTextController = TextEditingController();
RxString myName = ''.obs;
// For save value to local
saveValue() async {
await Storage.removeValue("name");
await Storage.saveValue("name", saveStorageTextController.text.trim());
}
// For get value from local
getValue() async {
myName.value = await Storage.getValue("name");
log("name :-> ${myName.value}");
}
}
save_storage_page.dart
class SaveStoragePage extends StatelessWidget {
const SaveStoragePage({super.key});
@override
Widget build(BuildContext context) {
SaveStorageController controller = Get.put(SaveStorageController());
return SafeArea(
child: Scaffold(
body: Padding(
padding: const EdgeInsets.all(18.0),
child: Obx(
() => Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(controller: controller.saveStorageTextController),
const SizedBox(
height: 10,
),
ElevatedButton(
onPressed: () {
controller.saveValue();
},
child: const Text("Save Value")),
Padding(
padding: const EdgeInsets.symmetric(vertical: 15.0),
child: ElevatedButton(
onPressed: () {
controller.getValue();
},
child: const Text("Get Value")),
),
Text("My Name is :-> ${controller.myName.value} ")
],
),
),
),
),
);
}
}