Do not re-instantiate the same values from the Database

Viewed 27

I have a class that can be instantiated in DART, as follows

class Tracker with ChangeNotifier {
  final double id;
  final String? trackerCode;
  final String? trackerName;
  final bool? isFavorite;
  final bool? isArchived;

  Tracker({
    required this.id,
    required this.trackerCode,
    required this.trackerName,
    required this.isFavorite,
    required this.isArchived,
  });

}

And I instantiate this class from the database, when the user accesses my main page, I call a method that makes a select in the database and instantiates all the information from the database, creating new instances of the classes, in this way

static Future<List> selectAllDatabaseIsArchivedInformations() async {
    final Database database = await _databaseCheck();

    List trackersAvailable = await database.query("Correios",
        columns: [
          "id",
          "tracker_id",
          "tracker_code",
          "tracker_name",
          "is_favorite",
          "is_archived"
        ],
        where: "is_archived=1",
        whereArgs: ["0"]);

    for (var tracker in trackersAvailable) {
      Tracker(
        id: tracker['id'],
        trackerCode: tracker['tracker_code'],
        trackerName: tracker['tracker_name'],
        isFavorite: tracker['is_favorite'] == 1 ? true : false,
        isArchived: tracker['is_archived'] == 1 ? true : false,
      );
    }
    return trackersAvailable;
  }

My question is, every time I go to the main page, this method that selects and instantiates new classes is called, doing it in a loop until the user closes the application. How do I make the class to be instantiated only once and have all the database information only once instantiated, not generating duplicate data?

0 Answers
Related