shared_preferences_content_provider

pub package

Shared preference supporting Android content provider

Note: Only support Android

Usage

To use this plugin, add shared_preferences_content_provider as a dependency in your pubspec.yaml file.

Example

Add this to AndroidManifest.xml

Host app (Provider app):
<manifest>
    <application>
         <provider
           android:name="com.zhgwu.shared_preferences_content_provider.SharedPreferencesContentProvider"
           android:authorities="com.zhgwu.shared_preferences_content_provider.example"
           android:readPermission="shared_preferences.permission.READ_TOKEN"
           android:writePermission="shared_preferences.permission.WRITE_DATA"
           android:enabled="true"
           android:exported="true" />
    </application>

   
</manifest>
Consumer app:
<manifest>
    <queries>
        <package android:name="com.zhgwu.shared_preferences_content_provider_example" />  <!--host app package-->
    </queries>
    <uses-permission android:name="shared_preference.permission.WRITE_DATA" />
    <uses-permission android:name="shared_preference.permission.READ_DATA" />
</manifest>
// The content provider must be set up first
await SharedPreferencesContentProvider.init(
    providerAuthority:
    'com.zhgwu.shared_preferences_content_provider.example',  //authority provider in AndroidManifest.xml
);
// Put values
await SharedPreferencesContentProvider.putBool('MY_BOOL_KEY', true);
await SharedPreferencesContentProvider.putString('MY_STRING_KEY', 'STRING_VALUE');
await SharedPreferencesContentProvider.putInt('MY_INT_KEY', 42);
await SharedPreferencesContentProvider.putDouble('MY_DOUBLE_KEY', 9.9);
// Get values
bool boolValue = await SharedPreferencesContentProvider.get('MY_BOOL_KEY');
String stringValue = await SharedPreferencesContentProvider.get('MY_STRING_KEY');
int intValue = await SharedPreferencesContentProvider.get('MY_INT_KEY');
double doubleValue = await SharedPreferencesContentProvider.get('MY_DOUBLE_KEY');
// Listen when value change
SharedPreferencesContentProvider.listen((event) {
    print(event);
  }, 
  key: '123', // If provide [key], only receive notify when value of [key] change
);