shared_preferences_wrapper 0.0.5 copy "shared_preferences_wrapper: ^0.0.5" to clipboard
shared_preferences_wrapper: ^0.0.5 copied to clipboard

A Flutter package that provides a simple wrapper for working with shared preferences. This package simplifies the process of storing and retrieving various data types.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:shared_preferences_wrapper/shared_preferences_wrapper_encryption.dart';
import 'package:shared_preferences_wrapper/shared_preferences_wrapper.dart';

void main() {
  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 MaterialApp(
      title: 'Shared Preferences Wrapper Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home:
          const MyHomePage(title: 'Shared Preferences Wrapper Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String stringValue = 'Yung';
  int intValue = 1;
  double doubleValue = 2.0;
  bool boolValue = true;
  List<String> myStringList = ['item1', 'item2', 'item3'];
  final myMap = {
    'name': 'Yung',
    'age': 30,
    'isStudent': true,
  };

  String text = '';

  String secretKey16Char = 'my16CharacterKey';
  String secretKey32Char = 'my32lengthsupersecretnooneknows1';

  void handleChangeListener() {
    print("Listener triggered!");
  }

  // Function to observe changes
  Function(String, dynamic) handleObserverChanges =
      (String key, dynamic newValue) {
    print("Observer triggered with data: key=$key value=$newValue");
  };

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      // set an encryption key, this has to be 16/24/32 character long
      // CURRENTLY ONLY STRING DATA TYPES ARE SUPPORTED
      //SharedPreferencesWrapperEncryption.setEncryptionKey('my16CharacterKey');

      // Registering Listeners with callback function for when a shared preference changes
      SharedPreferencesWrapper.addListener('key', handleChangeListener);

      // to trigger registered listeners callback
      Future.delayed(const Duration(seconds: 10), () {
        SharedPreferencesWrapper.addString('key', 'updated value');
        SharedPreferencesWrapper.addInt('int', 200);
      });

      // // storing values
      // // note: refer to the documentation for storing other data types
      await SharedPreferencesWrapper.addString('key', 'value');

      await SharedPreferencesWrapper.addInt('int', 100);

      // retrieve values
      final stringResult = await SharedPreferencesWrapper.getString('key');
      setState(() {
        text += '\nString value stored: $stringResult';
      });

      // // storing lists
      SharedPreferencesWrapper.addStringList('listkey', myStringList);
      // retrieving a list
      List<String> value =
          await SharedPreferencesWrapper.getStringList('listkey');

      setState(() {
        text += '\nList value stored: $value';
      });

      // // storing maps
      SharedPreferencesWrapper.addMap('mapkey', myMap);
      // retrieving a map
      Map<String, dynamic>? map =
          await SharedPreferencesWrapper.getMap('mapkey');

      setState(() {
        text += '\nStored Map values: $map';
      });

      // // note: refer to the documentation for storing other data types

      // // Adding multiple preferences at once
      Map<String, dynamic> dataToAdd = {
        'key1': 'value1',
        'key2': 42,
        'key3': true,
        'key4': ['item1', 'item2'],
        'key5': {'nestedKey': 'nestedValue'},
        // Add more key-value pairs as needed
      };
      await SharedPreferencesWrapper.addBatch(dataToAdd);
      // // access the batch data normally as you would,
      // //take note of the data type stored to call the correct corresponding method
      bool boolValue = await SharedPreferencesWrapper.getBool('key3');
      int intValue = await SharedPreferencesWrapper.getInt('key2');

      setState(() {
        text += '\n\nBatch Data: bool = $boolValue, int: = $intValue\n\n';
      });

      // // Updating existing preferences in batch
      Map<String, dynamic> dataToUpdate = {
        'key3': false,
        'key2': 100,
        // Update other keys as needed
      };
      await SharedPreferencesWrapper.updateBatch(dataToUpdate);

      bool boolValue1 = await SharedPreferencesWrapper.getBool('key3');
      int intValue1 = await SharedPreferencesWrapper.getInt('key2');
      setState(() {
        text += 'Updated Batch Data: bool = $boolValue1, int: = $intValue1\n\n';
      });

      // // Add preferences to a specific group
      await SharedPreferencesWrapper.addToGroup(
          'UserSettings', 'username', 'JohnDoe');
      await SharedPreferencesWrapper.addToGroup(
          'UserSettings', 'email', 'john@example.com');

      await SharedPreferencesWrapper.addToGroup(
          'AppSettings', 'darkMode', true);
      await SharedPreferencesWrapper.addToGroup(
          'AppSettings', 'language', 'English');

      // // Retrieve preferences from a specific group
      Map<String, dynamic>? userSettings =
          await SharedPreferencesWrapper.getGroup('UserSettings');
      Map<String, dynamic>? appSettings =
          await SharedPreferencesWrapper.getGroup('AppSettings');
      print('userSettings: $userSettings');
      print('appSettings: $appSettings');

      // Add an observer
      SharedPreferencesWrapper.addObserver('observer', handleObserverChanges);
      await SharedPreferencesWrapper.addString('observer', 'value1');

      // Remove an observer (when no longer needed)
      SharedPreferencesWrapper.removeObserver(
          'observer', handleObserverChanges);
      await SharedPreferencesWrapper.addString('observer', 'value2');

      await SharedPreferencesWrapper.setValue('token', '12345678910', expirationDuration: Duration(days: 1));
      final val = await SharedPreferencesWrapper.getValue('token');

      print('cached data: $val');
      Future.delayed(const Duration(seconds: 20), ()async {
        final data = await SharedPreferencesWrapper.getValue('token');
        print('cached data after 20 seconds: $data');
      });

      await SharedPreferencesWrapper.getBuilder().then((builder) => {
            builder
                .addBool('builder_bool', true)
                .addDouble('builder_double', 10.0)
                .addString('builder_str', 'str value')
                .addInt('builder_int', 100)
                .addMap('builder_map', {
              'name': 'Yung',
              'lname': 'Cet'
            }).addStringList('builder_list', ['item 1', 'item 2'])
          });

      final builder_bool =
          await SharedPreferencesWrapper.getValue('builder_bool');
      print('builder_bool: $builder_bool');

      final userPrefs = SharedPreferencesWrapper.createNamespace('user');
      await userPrefs.setValue('name', 'John Doe');

      String? userName = await userPrefs.getValue('name');
      print('userName: $userName');
      await userPrefs.clearNamespace();

      final appPrefs = SharedPreferencesWrapper.createNamespace('app');
      await appPrefs.setValue('dark_mode', true);

      bool? mode = await appPrefs.getValue('dark_mode');
      print('mode: $mode');

      // ENCRYPTIONS
      String pinKey = 'pin';
      String pin = '123456';
      await SharedPreferencesWrapper.addString(pinKey, pin,
          salsa20Encryption: Salsa20Encryption(encryptionKey: secretKey16Char));

      String? mypin = await SharedPreferencesWrapper.getString(pinKey,
          salsa20Decryption: Salsa20Decryption(encryptionKey: secretKey16Char));

      print('pin: $mypin');
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              text,
            ),
          ],
        ),
      ),
    );
  }
}
4
likes
150
points
55
downloads

Publisher

verified publisherpermanentlink.co.za

Weekly Downloads

A Flutter package that provides a simple wrapper for working with shared preferences. This package simplifies the process of storing and retrieving various data types.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

encrypt, flutter, shared_preferences

More

Packages that depend on shared_preferences_wrapper