save Map<String, dynamic> or json raw into flutter application - flutter

i'm looking for a solution to save data after a api call like userData, cartitem ecc..
I used sharedPreferences but i can't directly save the json or the Map<String, dynamic> inside it
just String or List.
I'd like to store all my data into one place and call single field using varible.field into my app, it is a correct way to build app and it can be done?

you can create data in sharedpreferences it is suggested to store small key value pair values in sharedpreferences. but if you have large size of data store it in the database like(SQFlite, Hive) which gives you a lot other features(applying filters on data, accessing data on some condition, making relations etc) in accessing the data.

Related

Store a list of objects from a stream in hive local db

I have this stream from firebase and i wanna store this product list in hive
static Stream<List<Products>> productStream(){
return Collections.products.snapshots().map(_productFromSnapshot);
}
Is there a better approach as this example?
Stream getProductStream = DbLogin.productStream();
getProductStream.listen((doc) {
List<Product> allProducts = [];
allProduct.addAll(doc);
Hive.box(HiveBoxes.allProductBox).addAll(allProduct);
});
There are a few ways to approach this, and the best approach may depend on your specific use case.
One approach would be to use the Hive.box method to create a local database, and then use the addAll method to add the data from your stream to the database.
Another approach would be to use the Hive.put method to store the data from your stream in a local file, and then use the get method to retrieve the data from the file.
Finally, you could also use the Hive.sync method to synchronize the data from your stream with a remote database.
Here is an example of how you could use the Hive.box method to create a local database and store the data from your stream:
Stream getProductStream = DbLogin.productStream();
getProductStream.listen((doc) {
List<Product> allProducts = [];
allProduct.addAll(doc);
Hive.box(HiveBoxes.allProductBox).addAll(allProduct);
});

Get stream of data from DB | Sembast | Flutter

I'm working with Sembast nowadays and was wondering If there's any way to create a stream of data that could get me all the values inside the DB. My requirement is to setup a listener on that stream so that whenever the data change is triggered, I could do something with it.
Documentation on Sembast is pretty limited and I'm now sure how I can do this. Usually I use the .find method to fetch all the values from within my db. I've been using a stringMapFactory to store my records.
Can we do this ? Any help would be really appreciated.
Sorry for the poor documentation.
It is quite similar to firestore. You can listen to all changes in a store
// Track every store changes
var query = store.query();
var subscription = query.onSnapshots(db).listen((snapshots) {
// snapshots always contains the list of all records
// ...
});
Basically you have a query on the store (with or without filter) that you can query or listen for changes.
See https://pub.dev/documentation/sembast/latest/sembast/QueryRef/onSnapshots.html
https://github.com/tekartik/sembast.dart/blob/master/sembast/doc/change_listener.md
If you use Hive as db, may use Hive Box as listenable.
ValueListenableBuilder<Box<YOUR_BOX_MODEL>>(
valueListenable: box.listenable(),
builder: (context,value,child){}
)

Getting download URLs from FirebaseStorage

Is there a way to get all the URLs of the files located in a specific firebase storage library?
Currently using the following method:
List<String> URLs = [];
final ListResult _files = await _fireStorage.ref('groups/').list();
for (var element in _files.items) {
String addable = await element.getDownloadURL();
URLs.add(addable);
}
MY problem is that I have lots of files located in the groups folder and this async call takes like a ton of time to fetch for all individual elements. Is there any API call to get the links in bulk? Or should I just store the links in a firebase document and fetch them from there?
Thanks in advance!
In order to successfully return the downloadURL the client has to ping the storage bucket. Firebase then has to check the security rules to ensure that the operation is allowed, then verify that the object exists, and then return it's download URL to you. Unfortunately there is currently no way of doing this process in bulk.
It's quite common to store the downloadURL in a Firestore document so you can avoid the additional overhead and retrieve the downloadURL directly. But remember, downloadURLs are public and do not adhere to security rules. So make sure the document you're storing them in has proper rules placed on it.
Remember to perform a clean up operation and remove the reference in Firestore if you every delete the file.

RxDart setup Map on stream

I got a setup with a BehaviorSubject. Whenever I load data from an API I call Add to push the data and use it from the stream in a page. Now I want to format some of the data before it is consumed from the stream. As far as I can see Map seems to be the thing to use for a transformation of data, but I'm not sure if it should be set up once or I need to ensure Map is called when data is when data is loaded from an. Currently, I expose a behaviorSubject but if I use map should I then Expose a new stream based on the map?
This is my current setup. From a StreamBuilder in the page I'm using myRides. I can see the applyUnits gets called but its like data is not on the stream anymore
final BehaviorSubject<FeedLoad> ridesSubject = new BehaviorSubject<FeedLoad>();
Observable<FeedLoad> get myRides => ridesSubject.stream.map((item)=> item.applyUnits());

What is better way to save cart data on flutter?

i have flutter app, i want to store cart data on phone, what should i use,
SQFlite or SharedPreferences? Cart data contain multi array on single product
While using SharedPreferences will reduce computing overhead and simplify code implementation, it has it's limitations. For instance, you can only store primitive key/value pairs so it wouldn't be suitable for storing complex data structures or large arrays. The other major disadvantage is that you can't search through it, you have to know the exact key you're looking for.
For a situation where there is no upper limit to the size of the cart you'd be better off storing the data in a database or in internal storage by writing your own cart data file.
First Use Maps<List<Product>> for maintaining data in app, next to save it you can convert it to JSON and store as String in SharedPreferences as cart.
To retrieve just decode in Map and use.
Make sure decoding and encoding should be used as async and don't return any value, so user can continue with cart without interruption.
Use hive! You can store map, lists and any Dart object there. It is encrypted and it has WEB support!
You could take a look at the Provider package to store the state of your cart.