Mobx.dart: Difference between ObservableList and List?

Created on 29 Mar 2019  路  5Comments  路  Source: mobxjs/mobx.dart

Whats the difference between

@observable
  ObservableList<MatchClass> listOfMatches = ObservableList<MatchClass>();

and

@observable
  List<MatchClass> listOfMatches = [];
discussion

Most helpful comment

+1 to what @rrousselGit mentioned. Additionally it is worth thinking of MobX as a system that reports change in values.

  • When you mark something as @observable, you are hinting your interest in the change in value for that field. So anytime that field changes value, MobX will notify the reactions.
  • A plain List<T> does not have observability built in. If you change the items, MobX cannot know that the list items changed. However an ObservableList<T> is aware of such changes, like adding, removal, modification of an item at an index, etc.
  • Use an @observable List<T> when you are sure you are only interested in reference changes to the entire list.
  • Use @observable ObservableList<T> when you not only care about reference changes to the list but also changes in the contents (add/remove/update).

HTH.

All 5 comments

List will not notify Mobx that a value has changed when you do:

listOfMatches[0] = match;

But both will work when you do:

listOfMatched = []

+1 to what @rrousselGit mentioned. Additionally it is worth thinking of MobX as a system that reports change in values.

  • When you mark something as @observable, you are hinting your interest in the change in value for that field. So anytime that field changes value, MobX will notify the reactions.
  • A plain List<T> does not have observability built in. If you change the items, MobX cannot know that the list items changed. However an ObservableList<T> is aware of such changes, like adding, removal, modification of an item at an index, etc.
  • Use an @observable List<T> when you are sure you are only interested in reference changes to the entire list.
  • Use @observable ObservableList<T> when you not only care about reference changes to the list but also changes in the contents (add/remove/update).

HTH.

Additionally, note that you can do:

List<Foo> foo = ObservableList<Foo>()

And whats the difference between

ObservableList<Foo> foo = ObservableList<Foo>()

and

List<Foo> foo = ObservableList<Foo>()

?

Using it as ObservableList<T> is preferred as you want to be explicit about the type of the list. If you want to hide the specific type, go with the List<T> approach. Although the consumers of your code may not know that an ObservableList<T> is being used. This is good to hide that detail for your consumers, say a third party library that only works with List<T>.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pavanpodila picture pavanpodila  路  5Comments

vishalchandra picture vishalchandra  路  6Comments

thedejifab picture thedejifab  路  5Comments

JCKodel picture JCKodel  路  3Comments

vadimtsushko picture vadimtsushko  路  5Comments