storeDevice static method

void storeDevice(
  1. ScpDevice device,
  2. String path
)

Implementation

static void storeDevice(ScpDevice device, String path) async {
  //read file
  var jsonData = await JsonStorage.readJson('$path');
  List<ScpDevice> devices = List<ScpDevice>.empty(growable: true);
  if (jsonData != null) {
    devices.addAll(ScpDevice.devicesfromJson(jsonData));
  } else {
    Scp.getInstance().log('File $path does not exist, creating it...');
  }
  //add to List, remove if it already exists to mitigate duplicates
  devices.removeWhere((element) => element.deviceId == device.deviceId);
  devices.add(device);
  //write List to JSON
  String encoded = jsonEncode(devices);
  final file = await File('$path');
  // Write the file
  file.writeAsString('$encoded', mode: FileMode.write);
}