0

For example, I have a class with a static field that can store my data, but I'm not sure if this is correct for OOP principles. What is the best way to do this?

class Data {
  static String token;
  static DoctorProfile doctor;
  static UserProfile userProfile;
}

1 Answers1

0

Its depends.

const: If the value is known at compile time you should use const.

Use const for variables that you want to be compile-time constants. If the const variable is at the class level, mark it static const. Where you declare the variable, set the value to a compile-time constant such as a number or string literal, a const variable, or the result of an arithmetic operation on constant numbers:

const bar = 1000000; // Unit of pressure (dynes/cm2)
const double atm = 1.01325 * bar; // Standard atmosphere

final: If you do not know the value at compile time, but it would be generated at runtime.

Final instance variables must be initialized before the constructor body starts.

class Data {
  final String token;
  final DoctorProfile doctor;
  final UserProfile userProfile;

  Data({this.token, this.doctor, this.userprofile});
}

Reference:

  1. Final and const 2.What is the difference between the “const” and “final” keywords in Dart?
Haniel Baez
  • 1,646
  • 14
  • 19