Last updated:
0 purchases
local shared
Local Shared | View Demo #
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 #
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 #
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 #
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 #
local_shared/example/lib/main.dart
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.