For example I have a field that I want to access in my app view. I have this provider

class User with ChangeNotifier{
   String userNick = "test";

   String get getName() => userNick;
}

What will be the difference, if I access the nick this way in my app vs getter?

context.watch<User>().userNick;

vs

context.watch<User>().getName();

If I don't use the getter and my userNick changes, will I not see it refreshing in my app or?


Solution 1: Eray

Getters/setters are preferred for use the data properly. If you use the data directly, you can update it when you even don't wanna update it and this type of logical mistakes takes too much time to detect and fix. Also, it is safe way to manipulate the data.


Encapsulation is an object-oriented programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse. Data encapsulation led to the important OOP concept of data hiding.

from: https://en.wikipedia.org/wiki/Object-oriented_programming


For more, you can search for encapsulation online.