jonataslaw / getx

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

Obx doesn't update ListView in GetWidget #970

Open mjablecnik opened 3 years ago

mjablecnik commented 3 years ago

Describe the bug In my application I am binding controller through Get.create() function and Page where I have the controller extends with GetWidget. Inside in this Page I have Obx with ListView builder which updates the list items. When I use bindings with lazyPut so Obx works right but when I change binding to Get.create() function so Obx doesn't update the ListView.

**Reproduction code Project: https://github.com/mjablecnik/flutter_getx_example Bindings: https://github.com/mjablecnik/flutter_getx_example/blob/master/lib/main.dart#L39 GetWidget: https://github.com/mjablecnik/flutter_getx_example/blob/master/lib/pages/jottings.dart#L15

To Reproduce Steps to reproduce the behavior: 1) Run in terminal:

git clone https://github.com/mjablecnik/flutter_getx_example.git
flutter pub get
flutter packages pub run build_runner build

2) Open this project in your IDE and run it in emulator 3) Click to button for add note, todo or folder 4) You can change bindings here for testing: https://github.com/mjablecnik/flutter_getx_example/blob/master/lib/main.dart#L39

Expected behavior Update ListView after add items in my app:

Video: https://user-images.githubusercontent.com/2686906/103431385-31c15780-4bcf-11eb-827e-e1cebe4e3bf3.mp4 (In the first build the ListView is updated but after second build not.)

Flutter Version: Flutter 1.22.5 • channel stable • https://github.com/flutter/flutter.git Framework • revision 7891006299 (3 weeks ago) • 2020-12-10 11:54:40 -0800 Engine • revision ae90085a84 Tools • Dart 2.10.4

Getx Version: ^3.24.0

Describe on which device you found the bug: Nexus 6 API 28 - Android

yasinarik commented 3 years ago

I am interested in this problem since I also have some problems with Get.create and GetWidget.

Would you mind adding a text widget to your list's items showing the controller.hashcode.toString() ?

I thought that Get.create() just creates a new instance of the controller and injects it. However, it just tells the GetWidget or anything using Get.find(): "Hey, Get.create() is used. So each time this controller is needed, just use a different, a new unique instance of it."

So, I might completely misunderstand the problem but I think all of your list items are using a different, new instance of the same type of controller.

I would rather prefer Get.create() to create and inject a new instance of the controller when it is executed, not when the controller is needed.

Of course, I need an explanation from @jonataslaw himself to understand what's going on. Btw, here are issues I opened. You might want to have a look at them #974 & #939

jonataslaw commented 3 years ago

I am currently traveling and I opened this up quickly on my laptop, and it seems to me that you are using GetWidget instead of GetView. GetWidget should be used in things like children of a ListView, and not (at least for now) a Substitute for StatelessWidget or StatefulWidget, not least because it is not based on either of the two Flutter widgets. Probably in version 4.0 you will be able to use it as a replacement, but things are not so in version 3.

mjablecnik commented 3 years ago

@jonataslaw I tried also to use GetView instead of GetWidget but behavior is still same with the same problem.. This is my change: https://github.com/mjablecnik/flutter_getx_example/commit/ef3f94c75bd6828ca8217fd928c8696ae800416c

eduardoflorence commented 3 years ago

Hi @mjablecnik, I looked at your code after switching to GetView. You cannot use Get.create for your use case, as you will not be able to share the controller with the dialogs and the view. Use Get.lazyPut:

initialBinding: BindingsBuilder(() {
 Get.lazyPut(() => BasicController(), fenix: true);
 Get.lazyPut(() => DialogController());
 Get.lazyPut(() => JottingsController());
}),
mjablecnik commented 3 years ago

@eduardoflorence I thought that Get.create create new controller everytime when Get.find is called. It's described here: https://github.com/jonataslaw/getx/blob/master/documentation/en_US/dependency_management.md#getcreate

And I want to create new instance controller everytime when I go to JottingsPage throw the folder in JottingsPage: https://github.com/mjablecnik/flutter_getx_example/blob/master/lib/controllers/jottings.dart#L58

jonataslaw commented 3 years ago

I think you didn't quite understand, if you need to create a controller every time you enter a page, you should use Get.lazyPut. Get.create is for when you want to use multiple controllers of the same type, within the same widget, as the items in a ListView, and not the ListView itself. You do not need and should not use Get.create + GetWidget in this situation

mjablecnik commented 3 years ago

@jonataslaw But when I use Get.find so it return me also same object which was once created or not?

https://user-images.githubusercontent.com/2686906/103696576-e1ffd900-4f9e-11eb-988a-972603680ef0.mp4

mjablecnik commented 3 years ago

Ok so finally I resolved my problem with this commit: https://github.com/mjablecnik/flutter_getx_example/commit/66f22b56a9e8d37d54f582097f307bba766e6fdb

Where I am not using GetX Bindings and DI but I am adding my own created controllers into the page through constructor.

ghost commented 3 years ago

I have the same problem, and it appeared after updating the Getx version from 3.22.2 to 3.24.0 After the update, Get.create + GetWidget began to work incorrectly. Somewhere it works, but somewhere it doesn't.

igorvidottof commented 3 years ago

I have the same problem, and it appeared after updating the Getx version from 3.22.2 to 3.24.0 After the update, Get.create + GetWidget began to work incorrectly. Somewhere it works, but somewhere it doesn't.

Same problem here... I have 6 list items, but only 5 controllers are initialized, which breaks the state management of the listview. Has anyone found a solution yet? Version 3.22.2 is working as expected though

eduardoflorence commented 3 years ago

I have the same problem, and it appeared after updating the Getx version from 3.22.2 to 3.24.0 After the update, Get.create + GetWidget began to work incorrectly. Somewhere it works, but somewhere it doesn't.

Same problem here... I have 6 list items, but only 5 controllers are initialized, which breaks the state management of the listview. Has anyone found a solution yet? Version 3.22.2 is working as expected though

Update GetX to 3.26.0

igorvidottof commented 3 years ago

I have the same problem, and it appeared after updating the Getx version from 3.22.2 to 3.24.0 After the update, Get.create + GetWidget began to work incorrectly. Somewhere it works, but somewhere it doesn't.

Same problem here... I have 6 list items, but only 5 controllers are initialized, which breaks the state management of the listview. Has anyone found a solution yet? Version 3.22.2 is working as expected though

Update GetX to 3.26.0

Thanks Eduardo! But I had already tried that.. Still not working. It creates the number of controllers - 1 :(