readSettings method

Future<Settings> readSettings({
  1. bool retry = true,
})

Reads the user's settings from the settingsFile.

Implementation

Future<Settings> readSettings({bool retry = true}) async {
  final Json json = jsonDecode(await settingsFile.readAsString());
  try {
    final settings = Settings.fromJson(json);
    await writeSettings(settings);  // re-save any default values
    return settings;
  } catch (error) {
    if (kDebugMode) {
      print("Here are the contents of the settings file: \n$json");
      rethrow;
    }
    services.error = "Settings were corrupted and reset back to defaults";
    await writeSettings(Settings.fromJson({}));  // delete corrupt settings
    if (retry) {
      return readSettings(retry: false);
    } else {
      rethrow;
    }
  }
}