LOCAL STORAGE

  • Steps

    1. pubspec.yaml

    
                  dependencies:
                    flutter:
                      sdk: flutter
                    get_storage: ^2.0.3  # Use the latest version
    
    
    

    2. import the necessary packages:

    
    import 'package:flutter/material.dart';
    import 'package:get_storage/get_storage.dart';
    
    

    3. Storing Data:

    
    class StorageController extends GetxController {
      final box = GetStorage();
    
      void saveData() {
        box.write('key', 'Hello, GetStorage!'); // Save data with a key
      }
    }
    
    

    4. Retrieving Data:

    
    class StorageController extends GetxController {
      final box = GetStorage();
    
      String? getData() {
        return box.read('key'); // Retrieve data using the key
      }
    }
    
    

    5. Using GetX Controller:

    
    void main() async {
      await GetStorage.init(); // Initialize GetStorage
    
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return GetMaterialApp(
          title: 'GetStorage Example',
          home: Scaffold(
            appBar: AppBar(
              title: Text('GetStorage Example'),
            ),
            body: Center(
              child: ElevatedButton(
                onPressed: () {
                  StorageController().saveData(); // Save data on button press
                  String? data = StorageController().getData(); // Retrieve data
                  print(data); // Print the retrieved data
                },
                child: Text('Save & Retrieve Data'),
              ),
            ),
          ),
        );
      }
    }