shared_preference_app_group 1.1.1 copy "shared_preference_app_group: ^1.1.1" to clipboard
shared_preference_app_group: ^1.1.1 copied to clipboard

PlatformiOS

Shared preference supporting iOS App Group capability (using `-[NSUserDefaults initWithSuiteName:]`)

example/lib/main.dart

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

import 'package:shared_preference_app_group/shared_preference_app_group.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  // Fill in the app group id set in the Xcode project
  String appGroupID = 'group.com.example.your_awesome_project';

  Map<String, dynamic> myParams = {
    'MY_BOOL_KEY': false,
    'MY_STRING_KEY': 'null',
    'MY_INT_KEY': 0,
    'MY_DOUBLE_KEY': 0.1
  };

  String displayParamsText = '';

  @override
  void initState() {
    super.initState();
    // The app group must be set up first
    SharedPreferenceAppGroup.setAppGroup(appGroupID);
  }

  void setMyParams() {
    SharedPreferenceAppGroup.setBool('MY_BOOL_KEY', true);
    SharedPreferenceAppGroup.setString('MY_STRING_KEY', 'STRING_VALUE');
    SharedPreferenceAppGroup.setInt('MY_INT_KEY', 42);
    SharedPreferenceAppGroup.setDouble('MY_DOUBLE_KEY', 9.9);
    SharedPreferenceAppGroup.setStringList('MY_STRING_ARRAY', ["element1", "element2", "element3"]);
  }

  Future<void> getMyParams() async {
    bool boolValue =
        await SharedPreferenceAppGroup.getBool('MY_BOOL_KEY') ?? false;
    String stringValue =
        await SharedPreferenceAppGroup.getString('MY_STRING_KEY') ?? 'null';
    int intValue = await SharedPreferenceAppGroup.getInt('MY_INT_KEY') ?? 0;
    double doubleValue =
        await SharedPreferenceAppGroup.getDouble('MY_DOUBLE_KEY') ?? 0.0;
    List<String> stringArrayValue =
        await SharedPreferenceAppGroup.getStringList('MY_STRING_ARRAY') ?? [];

    this.myParams = {
      'MY_BOOL_KEY': boolValue,
      'MY_STRING_KEY': stringValue,
      'MY_INT_KEY': intValue,
      'MY_DOUBLE_KEY': doubleValue,
      'MY_STRING_ARRAY': stringArrayValue
    };

    String text = '';
    for (String key in this.myParams.keys) {
      text += '$key = ${this.myParams[key]}\n';
    }

    setState(() {
      this.displayParamsText = text;
    });
  }

  Future<void> getAllParams() async {
    Map<String, dynamic> allPreferences = await SharedPreferenceAppGroup.getAll();

    String text = '';
    for (String key in allPreferences.keys) {
      text += '$key = ${this.myParams[key]}\n';
    }

    setState(() {
      this.displayParamsText = text;
    });
  }

  Future<void> removeMyParams() async {
    await SharedPreferenceAppGroup.remove('MY_BOOL_KEY');
    await SharedPreferenceAppGroup.remove('MY_STRING_KEY');
    await SharedPreferenceAppGroup.remove('MY_INT_KEY');
    await SharedPreferenceAppGroup.remove('MY_DOUBLE_KEY');
  }

  Future<void> removeAll() async {
    await SharedPreferenceAppGroup.removeAll();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Container(
            padding: const EdgeInsets.symmetric(horizontal: 40.0),
            child: ListView(
              children: [
                Padding(padding: const EdgeInsets.only(top: 50.0)),
                Text('Only support iOS'),
                Padding(padding: const EdgeInsets.only(top: 10.0)),
                Text('You should first enable the AppGroup capability for the Runner Target of your Xcode project, and then set the app group id to this plugin through `setAppGroup` function',
                  style: TextStyle(
                    fontSize: 11.0,
                  ),
                ),
                Padding(padding: const EdgeInsets.only(top: 10.0)),
                Container(
                  padding: const EdgeInsets.all(0.0),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(12.0),
                    color: Color(0xff0e88eb),
                  ),
                  width: 240.0,
                  height: 50.0,
                  child: CupertinoButton(
                    child: Text('SetParams',
                      style: TextStyle(
                        color: Colors.white
                      ),
                    ),
                    onPressed: setMyParams,
                  ),
                ),
                Padding(padding: const EdgeInsets.only(top: 10.0)),
                Container(
                  padding: const EdgeInsets.all(0.0),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(12.0),
                    color: Color(0xff0e88eb),
                  ),
                  width: 240.0,
                  height: 50.0,
                  child: CupertinoButton(
                    child: Text('RemoveParams',
                      style: TextStyle(
                        color: Colors.white,
                      ),
                    ),
                    onPressed: removeMyParams,
                  ),
                ),
                Padding(padding: const EdgeInsets.only(top: 10.0)),
                Container(
                  padding: const EdgeInsets.all(0.0),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(12.0),
                    color: Color(0xff0e88eb),
                  ),
                  width: 240.0,
                  height: 50.0,
                  child: CupertinoButton(
                    child: Text('RemoveAll',
                      style: TextStyle(
                        color: Colors.white
                      ),
                    ),
                    onPressed: removeAll,
                  ),
                ),
                Padding(padding: const EdgeInsets.only(top: 10.0)),
                Container(
                  padding: const EdgeInsets.all(0.0),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(12.0),
                    color: Color(0xff0b5eda),
                  ),
                  width: 240.0,
                  height: 50.0,
                  child: CupertinoButton(
                    child: Text('GetAllParams',
                      style: TextStyle(
                        color: Colors.white
                      ),
                    ),
                    onPressed: getAllParams,
                  ),
                ),
                Padding(padding: const EdgeInsets.only(top: 10.0)),
                Container(
                  padding: const EdgeInsets.all(0.0),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(12.0),
                    color: Color(0xff0b5eda),
                  ),
                  width: 240.0,
                  height: 50.0,
                  child: CupertinoButton(
                    child: Text('GetMyParams',
                      style: TextStyle(
                        color: Colors.white
                      ),
                    ),
                    onPressed: getMyParams,
                  ),
                ),
                Padding(padding: const EdgeInsets.only(top: 10.0)),
                Text(displayParamsText,
                  style: TextStyle(
                    fontSize: 11.0,
                  ),
                ),
              ],
            )
          )
        ),
      ),
    );
  }
}
19
likes
150
points
14.5k
downloads

Publisher

verified publisherpaaatrick.com

Weekly Downloads

Shared preference supporting iOS App Group capability (using `-[NSUserDefaults initWithSuiteName:]`)

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

flutter

More

Packages that depend on shared_preference_app_group