jonataslaw / getx

Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with Get.
MIT License
10.43k stars 1.63k forks source link

Need some clarifications #357

Closed psycura closed 4 years ago

psycura commented 4 years ago

Hello, and thank you for the great job, that you do.

I started to use your package in our App, and now routing & state management is much more readable and easy to maintain. However i have some questions and feature requests.

  1. I want to understand mechanism of put or lazyPut. I understand, that if i have some "local" controller (controller, that using only at specific page/pages), that shouldn't share state across multiple screens - in such case i can use Bindings in routes, and also put or lazyPut isn't important, because of mechanism of selfcreate/selfdispose.

But what about case, when i have controller, that should share state between different screens? Right now i create initialBinding that i put inside GetMaterialApp widget. And there i have issue when use lazyPut - i start receive errors in console, that controller doesn't initialised. But if i change lazyPut to put, everything works fine. Why is this happen?

Also what do you think the right way, of creating/put controller that should share state between set of specific screens only, and i dont want to add it into initialBinding? And also can i dispose controller manually?

  1. Feature request is related to workers. Right now we have 4 workers (ever, once, debounce, interval). First - is this possible to add option to pass as first argument List of states/dependencies? And worker will fire on change in each of them? Second - What do you think about worker similar to once, but in difference from once will accept some logical statement as first argument, for example state.value==true, and will be disposed after first fire. I know, that i can use every, and make a related check inside callback, but i don't want to continue listen to this events anymore.

  2. About your key/value storage: How its compared to Hive? Can i create/use different boxes? Can i store complex objects, and not only primitives?

And again, Thank you for the great work, that you do.

jonataslaw commented 4 years ago

Hello, and thank you for the great job, that you do. Thanks I started to use your package in our App, and now routing & state management is much more readable and easy to maintain. However i have some questions and feature requests.

  1. I want to understand mechanism of put or lazyPut. I understand, that if i have some "local" controller (controller, that using only at specific page/pages), that shouldn't share state across multiple screens - in such case i can use Bindings in routes, and also put or lazyPut isn't important, because of mechanism of selfcreate/selfdispose.

But what about case, when i have controller, that should share state between different screens? Right now i create initialBinding that i put inside GetMaterialApp widget. And there i have issue when use lazyPut - i start receive errors in console, that controller doesn't initialised. But if i change lazyPut to put, everything works fine. Why is this happen?

Basically, lazyPut will start up the first time someone uses a "find" on that dependency. The put will put it in memory immediately. I think the biggest question concerns SmartManagement. Using initialBindings, you can change your GetMaterialApp to smartManagement.keepFactory and you will never have errors in your app again. This is definitely the "easiest" option to use. But for those who want highly advanced control of what is inserted and removed from memory, using individual bindings with the default mode is better. Sometimes I think of changing this to the default just because it is infinitely easier. I use full mode, because I know the lib well enough, but definitely, smartManagement is smart but boring.

Using Get.put() or Get.lazyPut() in individual Bindings also has little difference, since put will only be inserted into memory when the page is built, while lazyPut will be called when the widget using that controller was built. You can maintain a permanent controller with "permanent: true" in the put.

Also what do you think the right way, of creating/put controller that should share state between set of specific screens only, and i dont want to add it into initialBinding?

You can do this by default. But keep in mind that if the first page that used that controller comes out of memory, it will automatically be placed in full mode. In keepFactory mode you can simply use Obx (which does not take care of the driver layout, just updating the screen), and when you have a screen you think: I want the driver to be laid out here. Simply give GetX an "assignId", and it will delete that widget as soon as the page using that widget is removed from memory. There are many concepts to cover here, but I think this is a start.

And also can i dispose controller manually?

Using Get.delete() Or signing it on a GetX

GetX<Controller>(
  assignId: true,
  builder: (_) => Text(_. name.value)
);
  1. Feature request is related to workers. Right now we have 4 workers (ever, once, debounce, interval). First - is this possible to add option to pass as first argument List of states/dependencies? And worker will fire on change in each of them?

That would actually be a few lines of code. I'm going to do a page on "customs Workers". They are really easy to do. What you asked for is basically a list.forEach((value) => ever(value), callback);

If you put this in a role, there you go, you already have custom Workers.

Second - What do you think about worker similar to once, but in difference from once will accept some logical statement as first argument, for example state.value==true, and will be disposed after first fire. I know, that i can use every, and make a related check inside callback, but i don't want to continue listen to this events anymore.

The third item of a worker is a condition. You can set a condition for that Worker to be triggered.

  1. About your key/value storage: How its compared to Hive?

I think it has different use cases than Hive. Hive is a database, has indexes, and the like. Get_storage is a key value store. This means that if you need indexes, structured data, or anything related to the database, you should use Hive. If you want to store key/value pairs, GetStorage handles this well, and with incredible performance, because the operations are done in memory. In Hive if you do something like

box.put('key', 'value');
box.put('key', 'value');
box.put('key', 'value');

You will add a key and a value to the end of the file each time you do this. With get_storage, key will be replaced, because you can only have one single key in the entire file. So each one has its advantage in use. get_storage is better for simple data storage and Hive is much better for structuring complex data. If you need a database: Hive, no doubt. If you need a key / value (to store user data, cache http requests, dark mode, or anything like that), get_storage. I use both in my projects, often together.

Can i create/use different boxes?

Yes:

final box1 = GetStorage('foo');
final box1 = GetStorage('bar');
GetStorage.init('foo'); 
GetStorage.init('bar');

Can i store complex objects, and not only primitives?

You can store int, String, num, double, Map, and List. If you want to store Objects, you will need to provide a "toJson" method and make the class Map before storing.

And again, Thank you for the great work, that you do.

Thanks

psycura commented 4 years ago

Thanks for quick reply. Very helpful

jonataslaw commented 4 years ago

It's nothing, Based on your answer I am closing this.