Kexi He
Kexi He

Reputation: 71

Why Static field can't be accessed through an instance

Why Static field can't be accessed through an instance. Dart 2.4 Flutter 1.7 Android Studio 3.4

When I was trying to port codes from JAVA to Flutter(Dart), I got the compile error

I had defined the variable in MyMainBloc as follows

static LoginStatus loginStatus = null;

Then I create an instance:

MyMainBloc myApp;

I expect to use something like this: myApp.loginStatus, NOT MyMainBloc.loginStatus.

Upvotes: 2

Views: 2296

Answers (1)

Anthony Andras
Anthony Andras

Reputation: 46

Static members of a particular class are accessed at the class level, not the object level. What this basically means is that the static variable is shared between objects.

With that in mind, what you are potentially attempting to do is probably not the best idea. Without seeing more of your code, you are probably better off declaring your loginStatus property as a private member on an encapsulating class, then accessing that member through a method (perhaps a standard get*() method).

The Oracle documentation on class members could be of some help here.

Upvotes: 2

Related Questions