filiph / state_experiments

Companion repository to the "Build reactive mobile apps in Flutter" talk
https://www.youtube.com/watch?v=RS36gBEp8OI
910 stars 134 forks source link

What is the purpose of Unmodifiable ListView ? #5

Closed ejabu closed 6 years ago

ejabu commented 6 years ago

https://github.com/filiph/state_experiments/blob/19c321bbc62ac10855751124e3ea9701e583d6ea/shared/lib/common/models/cart.dart#L36

I prefer to try bloc_start architecture.

Is it okay to change this to normal ListView ?

What is the purpose of this actually ?

filiph commented 6 years ago

Oh, definitely okay to use List.

UnmodifiableListView is there for safety. It's immutable, so consumers of the Cartclass cannot change the items in the class via the Cart.items getter. They have to go through Cart.add() and Cart.remove() (which make sure to do the right thing).

So, this is just a bit of encapsulation. For a smaller project and for learning purposes, it's okay to use a List.

I'm closing this but feel free to reopen if it's not clear or helpful.