local_shared 1.0.5 copy "local_shared: ^1.0.5" to clipboard
local_shared: ^1.0.5 copied to clipboard

A SharedPreferences wrapper for JSON file-based storage, providing an alternative to Localstore. Safely execute CRUD operations on any platform local storage.

Inidia.app Local Shared Logo

Local Shared | View Demo #

LocalShared.gif

A SharedPreferences wrapper for JSON file-based storage, providing an alternative to Localstore. Safely execute CRUD operations on any platform local storage.

Install #

Add this line to your pubspec.yaml.

dependencies:
  local_shared: ^1.0.5
copied to clipboard

Initialize #

Start by initializing the LocalShared instance in your main function, after ensuring Flutter is properly initialized.

import 'package:local_shared/local_shared.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await LocalShared('db').initialize();
}
copied to clipboard

and next, interact with collections and documents using either the LocalShared or Shared prefix. You can even create a custom definition for added convenience.

typedef DB = LocalShared;
copied to clipboard

Collection | View Code #

LocalShared Collection.gif

This guide illustrates fundamental CRUD (Create, Read, Update, Delete) operations for collection management. Interacting with it can be achieved through the following methods: Shared.col(id) or Shared.collection(id).

Create #

To initiate the creation of a new collection, utilize this method:

final result = await Shared.col('myCollection').create();
print(result); // SharedMany(success: true, message: '...', data: <JSON>[])
copied to clipboard

Read #

To retrieve information pertaining to a collection, invoke this method:

final response = await Shared.col('myCollection').read();
print(response); // SharedMany(success: true, message: '...', data: <JSON>[])
copied to clipboard

Update #

To migrate or change collection id implement this method:

final response = await Shared.col('myCollection').update('myNewCollection');
print(response); // SharedMany(success: true, message: '...', data: <JSON>[])
copied to clipboard

Delete #

To remove a collection, employ this method:

final response = await Shared.col('myNewCollection').delete();
print(response): // SharedNone(success: true, message: '....')
copied to clipboard

Document | View Code #

LocalShared Document.gif

This guide elaborates on the essential CRUD (Create, Read, Update, Delete) operations for document management within collections. Interacting with document can be achieved through the following methods: Shared.col(id).doc(id) or Shared.collection(id).document(id).

Create #

To initiate the creation of a new document, leverage this method:

final result = await Shared.col('myCollection').doc('documentId').create({'key': 'value'});
print(result); // SharedOne(success: true, message: '...', data: JSON)
copied to clipboard

Read #

To retrieve the contents of a document within a collection, use this method:

final response = await Shared.col('myCollection').doc('documentId').read();
print(response); // SharedOne(success: true, message: '...', data: JSON)
copied to clipboard

Update #

To modify the contents of a document within a collection, invoke this method:

final response = await Shared.col('myCollection').doc('documentId').update({'newKey': 'newValue'});
print(response); // SharedOne(success: true, message: '...', data: JSON)
copied to clipboard

Delete #

To delete a document within a collection, implement this method:

final response = await Shared.col('myCollection').doc('documentId').delete();
print(response): // SharedNone(success: true, message: '...')
copied to clipboard

Many Document | View Code #

LocalShared Many Document.gif

This guide details the fundamental CRUD (Create, Read, Update, Delete) operations for the management of multiple documents within collections. Interacting with this can be achieved through the following methods: Shared.col(id).docs([id1, id2]) or Shared.collection(id).documents([id1, id2]).

Create #

Initiate the creation of multiple documents with this method:

final result = await Shared.col('myCollection').docs(['docId1', 'docId2']).create((index) => {'key': 'value'});
print(result); // SharedMany(success: true, message: '...', data: <JSON>[])
copied to clipboard

Read #

Retrieve the contents of multiple documents within a collection using this method:

final response = await Shared.col('myCollection').docs(['docId1', 'docId2']).read();
print(response); // SharedMany(success: true, message: '...', data: <JSON>[])
copied to clipboard

Update #

Modify the contents of multiple documents within a collection with this method:

final response = await Shared.col('myCollection').docs(['docId1', 'docId2']).update((index) => {'newKey': 'newValue'});
print(response); // SharedMany(success: true, message: '...', data: <JSON>[])
copied to clipboard

Delete #

Delete multiple documents within a collection using this method;

final response = await Shared.col('myCollection').docs(['docId1', 'docId2']).delete();
print(response); // SharedNone(success: true, message: '...')
copied to clipboard

Stream #

LocalShared offers a JSON stream for observing changes in collections when we access those collection through Shared.col or Shared.collection syntax. And if you interact with multiple collections, the stream exclusively displays data from the latest collection you engage with.

LocalShared.stream.listen(print);

await Shared.col('myCollection').docs(['A','B']).create((index) => {'desc': 'test'});
copied to clipboard

Result:

{id: myCollection, documents: [ {id: A, data: {desc: test}}, {id: B, data: {desc: test}} ]}

Custom Stream #

If you only want to observe changes in a specific collection, you can use the following approach:

final controller = StreamController<JSON>.broadcast();
final collection = SharedCollection('myCertainCollection', controller: controller);

controller.stream.listen(print);

await collection.docs(['A','B']).create((index) => {'desc': 'test'});
copied to clipboard

Result:

{id: myCertainCollection, documents: [ {id: A, data: {desc: test}}, {id: B, data: {desc: test}} ]}

Extension #

To simplify matters, there's an extension to handle response data from SharedResponse. If you're expecting SharedResponse to return JSON?, call this method:

JSON? result = await Shared.col(id).doc(id).read().one();
copied to clipboard

Alternatively, if you're expecting SharedResponse to return a List<JSON>?, call this method:

List<JSON>? result = await Shared.col(id).read().many();
copied to clipboard

Example #

16
likes
150
points
61
downloads
screenshot

Publisher

verified publisherinidia.app

Weekly Downloads

2024.10.02 - 2025.04.16

A SharedPreferences wrapper for JSON file-based storage, providing an alternative to Localstore. Safely execute CRUD operations on any platform local storage.

Homepage
Repository (GitHub)
View/report issues

Topics

#utility #storage #database #cache

Documentation

API reference

Funding

Consider supporting this project:

ko-fi.com

License

MIT (license)

Dependencies

flutter, shared_preferences

More

Packages that depend on local_shared