0

Tldr: is there an optional immutablity/unmodifiability in dart/flutter?

Similar to unmodifiableList I would love to have something like myHugeObject.unmodifiable(). Is that possible?

I am aware of immutability and libraries like freezed.

Desired traits:

  • mutable class (since it is enormous and I don't want to deal with tracking where the most up to date object is, I want at its core a mutable class)
  • I do something to that class to turn into an immutable/unmodifiable being, e.g. calling .unmodifiable that does not allow any action to be taken on that object
  • Only having this support in the linter would be fine by me (like @immutable in dart

My specific reason: I have a huge app data object that gets modified by different StateControllers. They should obviously have write access to the object. On the other hand, on specific events, I retrieve that object in the widget and use the data to build some widgets (rebuild happens on events, not the data change). Thus I want the UI to have read-only-access.

Long story short: is there some (possibly lint-only) way to have this functionality in flutter/dart. If not, do you have a reason why? (ImmutableClass.mutable() would also work btw)

superfuzzy
  • 346
  • 2
  • 13

0 Answers0