### Initialize and Use Get Storage for Theme Management Source: https://pub.dev/packages/get_storage/example Demonstrates initializing Get Storage and using it to manage a dark mode theme in a Flutter application. Requires `get_storage` and `get` packages. ```dart import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:get_storage/get_storage.dart'; void main() async { await GetStorage.init(); runApp(App()); } class Controller extends GetxController { final box = GetStorage(); bool get isDark => box.read('darkmode') ?? false; ThemeData get theme => isDark ? ThemeData.dark() : ThemeData.light(); void changeTheme(bool val) => box.write('darkmode', val); } class App extends StatelessWidget { @override Widget build(BuildContext context) { final controller = Get.put(Controller()); return Observer(builder: (_) { return MaterialApp( theme: controller.theme, home: Scaffold( appBar: AppBar(title: Text("Get Storage")), body: Center( child: SwitchListTile( value: controller.isDark, title: Text("Touch to change ThemeMode"), onChanged: controller.changeTheme, ), ), ), ); }); } } ``` -------------------------------- ### Install packages with Flutter Source: https://pub.dev/packages/get_storage Run this command in your terminal to install the package after adding it to your pubspec.yaml. ```bash $ flutter packages get ``` -------------------------------- ### SharedPreferences Implementation with GetStorage Source: https://pub.dev/packages/get_storage Example of implementing SharedPreferences-like functionality using GetStorage with custom classes and extension methods for cleaner syntax. ```dart class MyPref { static final _otherBox = () => GetStorage('MyPref'); final username = ''.val('username'); final age = 0.val('age'); final price = 1000.val('price', getBox: _otherBox); // or final username2 = ReadWriteValue('username', ''); final age2 = ReadWriteValue('age', 0); final price2 = ReadWriteValue('price', '', _otherBox); } ... void updateAge() { final age = 0.val('age'); // or final age = ReadWriteValue('age', 0, () => box); // or final age = Get.find().age; age.val = 1; // will save to box final realAge = age.val; // will read from box } ``` -------------------------------- ### Import get_storage in Dart code Source: https://pub.dev/packages/get_storage Import the get_storage library into your Dart files to start using its features. ```dart import 'package:get_storage/get_storage.dart'; ``` -------------------------------- ### Get GetStorage instance Source: https://pub.dev/packages/get_storage Obtain an instance of GetStorage to interact with the storage. You can use this instance or directly call static methods like read. ```dart final box = GetStorage(); ``` -------------------------------- ### get_storage pubspec.yaml Dependency Source: https://pub.dev/packages/get_storage/install This is an example of how the get_storage dependency will appear in your pubspec.yaml file after running `flutter pub add get_storage`. ```yaml dependencies: get_storage: ^2.1.1 ``` -------------------------------- ### Import get_storage in Dart Source: https://pub.dev/packages/get_storage/install Import the get_storage package into your Dart code to start using its functionalities. This import statement makes the package's classes and methods available. ```dart import 'package:get_storage/get_storage.dart'; ``` -------------------------------- ### Listen to all storage changes Source: https://pub.dev/packages/get_storage Use the `listen` method to subscribe to all changes in the storage. It returns a function to dispose of the listener. ```dart Function? disposeListen; disposeListen = box.listen((){ print('box changed'); }); ``` -------------------------------- ### Initialize GetStorage storage driver Source: https://pub.dev/packages/get_storage Initialize the GetStorage driver in your main function before running your application. This is an asynchronous operation. ```dart main() async { await GetStorage.init(); runApp(App()); } ``` -------------------------------- ### Initialize a specific named container Source: https://pub.dev/packages/get_storage Initialize a specific named container using `GetStorage.init` with the container's name. This should be done before accessing the container. ```dart await GetStorage.init('MyStorage'); ``` -------------------------------- ### Add get_storage to pubspec Source: https://pub.dev/packages/get_storage Add the get_storage package as a dependency in your pubspec.yaml file. ```yaml dependencies: get_storage: ``` -------------------------------- ### Create and use named GetStorage containers Source: https://pub.dev/packages/get_storage Instantiate GetStorage with a name to create separate storage containers. This allows for managing different sets of data independently. ```dart GetStorage g = GetStorage('MyStorage'); ``` -------------------------------- ### Add get_storage Dependency Source: https://pub.dev/packages/get_storage/install Use this command to add the get_storage package as a dependency to your Flutter project. This command automatically updates your pubspec.yaml file. ```bash $ flutter pub add get_storage ``` -------------------------------- ### Listen to changes on a specific key Source: https://pub.dev/packages/get_storage Use `listenKey` to subscribe to changes for a particular key. The callback receives the new value. ```dart box.listenKey('key', (value){ print('new key is $value'); }); ``` -------------------------------- ### Write data to GetStorage Source: https://pub.dev/packages/get_storage Use the `write` method to store key-value pairs in GetStorage. The value can be of various types. ```dart box.write('quote', 'GetX is the best'); ``` -------------------------------- ### Read data from GetStorage Source: https://pub.dev/packages/get_storage Use the `read` method to retrieve a value associated with a specific key. If the key does not exist, it returns null. ```dart print(box.read('quote')); // out: GetX is the best ``` -------------------------------- ### Dispose of storage listener Source: https://pub.dev/packages/get_storage Call the function returned by `listen` to unsubscribe from storage change events and prevent memory leaks. ```dart disposeListen?.call(); ``` -------------------------------- ### Import Statement in storage_impl.dart Source: https://pub.dev/packages/get_storage/score The import statement triggering a deprecation warning in the package's HTML storage implementation. ```dart ╷ 4 │ import 'dart:html' as html; │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ╵ ``` -------------------------------- ### Remove a key from GetStorage Source: https://pub.dev/packages/get_storage Use the `remove` method to delete a specific key-value pair from the storage. ```dart box.remove('quote'); ``` -------------------------------- ### Erase all data from GetStorage Source: https://pub.dev/packages/get_storage Use the `erase` method to remove all key-value pairs from the storage. ```dart box.erase(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.