libxd 1.0.0 copy "libxd: ^1.0.0" to clipboard
libxd: ^1.0.0 copied to clipboard

retracted

A libx-esque Collection infrastructure for MobX applications.

libxd #

A libx-esque Collection infrastructure for MobX applications. Based off of libx by jeffijoe.

Collection #

"I hate regular lists, I'm always getting duplicate items" - 733T-Hck3r-man-27

"How can anyone possibly maintain a list of objects and dynamically update them in realtime?!" - Tom Cruise

Well not to fear my friends, libxd is hear!

libxd maintains a collection of items, making sure we only have a single instance of an entity in memory. It even uses Mobx! That way, updates to an entity will propagate to the entire system without us having to do anything at all.

Getting started #

Be sure to install the latest build_runner pakcage and run flutter packages pub run build_runner build. This is used to generate the needed mobx_codegen files.

Why use Collections? #

The libxd Collection allows us to easly manage a list of any model type. It can ensure each item is unique by the models ID.

libxd uses the mobx_dart packge to ensure changes to the collection items can be observed and can propogate on your app realtime.

Specifying a Model ID #

Each item is required to have an ID variable. By default libxd tries to access the id param on your object (aka item.id). You can specify which field is your ID by overriding the getModelId method when instantiating your Colelction.

Collection<Car> cars = Collection<Car>(
    getModelId: (c) => c.vinNumber
);

Now everytime you add a new Car to your Collection it will ensure it is unique by it's VIN number.

Setting Objects #

Ok, so how do we go about updating and adding things to our Collection?

For example, lets say we have a User model:

class User {
    String id;
    String name;
    String email;
}

We can easily add items and update items in our collection by using the set or setAll methods:

Collection<User> users = Collection<User>();

final someApiRequest = await dio.get('/users');
final result = someApiRequest.data.map((json) => User.fromJson(json));

users.setAll(result);

This will add all User objects to our collection and update any duplicates.

Note: we don't need to specify getModelId since our class has an id field.

Sorting the Collection #

Say I have a list of objects that need to be ordered based on a specific field. Well libx does that!

Simply specify the sortBy field when instantiating a new Collection:

Collection<Messages> messages = Collection<Messages>(
    sortBy: CollectionSort<Messages>((m) => m.dateSent.toString(), 'desc'),
);

Now, whenerver the items in the collection are updated, the collection will be sorted to it's proper order.

"Ok, but I don't want to sort everytime the collection is updated..." - anonymous user

Well not to fear, we got you covered!

Just call sort on you collection and specify the field to be sorted on.

notifications.sort((n) => n.dateCreated.toString(), 'asc');

Removing Objects #

libxd makes it super simple to remove objects from your collection.

Remove by Reference #

final item = SomeItem();
collection.set(item);

collection.remove(item);

Remove by ID #

collection.removeById(item.userId);

Clearing the Collection #

Emptys the collection and preps it to recieve new items.

collection.clear()

Some nice to have methods #

Move Item #

"I'm implementing some sweet drag n' drop funcitonality, but I have to do this convoluted code to move things around in my list... pls help!" - noob coder

Not to worry libxd has your back!

Simply call move and specify your from index and to index.

collection.move(2, 7);

Original libx Package #

Creator: jeffijoe

Github: https://github.com/jeffijoe/libx

4
likes
0
pub points
17%
popularity

Publisher

unverified uploader

A libx-esque Collection infrastructure for MobX applications.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

collection, flutter, mobx

More

Packages that depend on libxd