-
-
Notifications
You must be signed in to change notification settings - Fork 309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ObservableList is not detected by Observer #851
Comments
I also tried to wrap it into |
I cannot figure out your full code ... but I think you need to annotate the
|
No, if I annotate it with Also, I don't use a code generation in my project and avoid any annotations. |
Got it! |
Also, to recreate a list like @observable
List<Todo> todos; The Moreover, it's one of the reasons why I ever use MobX. |
May be add method of ObservableList is not implemented so it is not reactive, i.e. acting like plain add method. Need to check the source code of ObservableList. |
It is implemented
so there should be a bug. |
according to this topic #765 |
In that case, builder has a new context. There should be a new observer. In other words, there will be no reaction if there is not an observer in the immediate context. |
But I cannot have an Observer inside of YandexMap, because it's a side-package. I cannot get into its source code and add an Observer. Also, as I mentioned above, |
Can you provide the minimal reproducible code as a gist ? I don't think there is any bug in mobx here. There must be some missing implementation. |
Hi @pr-1. I prepared a minimal app, showing the problem. https://github.com/subzero911/mobx_observableList_bug_demo/tree/master/lib If I directly use ListView inside of Observer, everything works fine. But if I would use |
Thanks for the code @subzero911 Observer(builder: (_) {
print('rebuilt');
return SizedBox(
width: 1024,
height: 512,
child: ChildWidget(
heartsList: controller.heartsList,
),
);
}), class ChildWidget extends StatelessWidget {
const ChildWidget({super.key, required this.heartsList});
final ObservableList<String> heartsList;
@override
Widget build(BuildContext context) {
return ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: heartsList.length,
itemBuilder: (context, index) {
return SizedBox(
width: 112,
height: 48,
child: ListTile(
title: Text(heartsList[index]),
),
);
});
}
}
The reactive context here will change and mobx will throw Instead make the child widget expect a Observer(builder: (_) {
print('rebuilt');
return SizedBox(
width: 1024,
height: 512,
child: ChildWidget(
heartsList: controller.heartsList.toList(),
),
);
}), class ChildWidget extends StatelessWidget {
const ChildWidget({super.key, required this.heartsList});
final List<String> heartsList;
@override
Widget build(BuildContext context) {
return ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: heartsList.length,
itemBuilder: (context, index) {
return SizedBox(
width: 112,
height: 48,
child: ListTile(
title: Text(heartsList[index]),
),
);
});
}
}
This is the reason why it is working as expected on the other branch . You can test it by making the List inside Child Widget a Similarly for YandexMap try using this because YandexMap is not expecting an Observer(builder: (_) {
print('rebuilt');
return SizedBox(
width: 1024,
height: 512,
child: YandexMap(
mapObjects: _placesController.mapObjects.toList(),
),
);
}), |
Thanks, that actually works! |
One of you should really add this to the docs... learned a bunch of things in this thread. |
I have the following code, where
mapObjects
is an ObservableList:Output in the console:
The declaration of ObservableList
I don't need to wrap it into
Observable<T>
, as I never change the actual list reference, but the list content only:I believe an Observer should be triggered and cause the rebuild, but it doesn't happen.
Sorry for comments in Russian, just ignore them.
May be related to this discussion #765
The text was updated successfully, but these errors were encountered: