Morez
Morez

Reputation: 2229

How can I create a non-modifiable class Flutter

I need to create a class called GeneralAppAndDeviceDetails which has the following fields:

import 'package:flutter/material.dart';

class GeneralAppAndDeviceDetails {
  final bool isDarkModeEnabled;
  final double deviceWidth;
  final double deviceHeight;
  final Color primaryColor;
  final Color secondaryColor;
}

It basically stores general device and app information at the start of the program so that I don't need to create a new instance of Theme or MediaQuery class whenever I want to access these details.

The problem I'm facing is that how can I write this class so that after the fields' values are assigned, They will be unmodifiable? (so that nothing can change the field values)

(I tried to create a singleton class but I need to pass the values to the constructor and by using factory and private constructor, A user can create new classes with different parameters passed to the factory.)

The thing I need is to have static fields that can receive a value once and become unmodifiable after that. How can I achieve something similar?

Thank you

Update:

I wrote the class as below:

import 'package:flutter/material.dart';

class GeneralAppAndDeviceDetails {
  final bool isDarkModeEnabled;
  final double deviceWidth;
  final double deviceHeight;
  final Color primaryColor;
  final Color secondaryColor;
  static bool _isAlreadyCreated = false;
  static GeneralAppAndDeviceDetails _instance;

  factory GeneralAppAndDeviceDetails(bool isDarkModeEnabled, double deviceWidth,
      double deviceHeight, Color primaryColor, Color secondaryColor) {
    if (_isAlreadyCreated == false) {
      _isAlreadyCreated = true;
      _instance = GeneralAppAndDeviceDetails._(isDarkModeEnabled, deviceWidth,
          deviceHeight, primaryColor, secondaryColor);
    }
    return _instance;
  }
  const GeneralAppAndDeviceDetails._(this.isDarkModeEnabled, this.deviceWidth,
      this.deviceHeight, this.primaryColor, this.secondaryColor);
}


I use a flag to check if an instance was created before or not in here and with this code, a similar instance will be returned every time but is it the best way to achieve this?

Upvotes: 0

Views: 253

Answers (2)

Arpit Awasthi
Arpit Awasthi

Reputation: 511

This is your singleton class

class Test{
  
  final String str;
  
  static Test _singleton;
  Test._internal({this.str});
  factory Test(String str) {
    return _singleton ??= Test._internal(
    str: str
    );
  }
  
}

example code for you to try and test

void main() {
  Test obj = Test('ABC');
  print(obj.str);
  Test obj1 = Test('XYZ');
  print(obj1.str);
}

class Test{
  
  final String str;
  
  static Test _singleton;
  Test._internal({this.str});
  factory Test(String str) {
    return _singleton ??= Test._internal(
    str: str
    );
  }
  
}

try running this in dartpad for better understanding

Upvotes: 1

Vilsad P P
Vilsad P P

Reputation: 1559

you can make the class as singleton and then make these fields as private, accessible only through getters and setters, inside the setter you can check and discard the new value if there is already some value assigned to the field.

Upvotes: 1

Related Questions