importFromJson method

Future<void> importFromJson(
  1. String source
)

Imports data from a JSON file or string

source is the path to the file or the JSON string to import from

Implementation

Future<void> importFromJson(String source) async {
  String jsonData;

  try {
    // Try to read from a file
    final file = File(source);
    if (await file.exists()) {
      jsonData = await file.readAsString();
    } else {
      // If it's not a file, assume it's a JSON string
      jsonData = source;
    }
  } catch (_) {
    // If we can't read from a file, assume it's a JSON string
    jsonData = source;
  }

  try {
    final data = jsonDecode(jsonData) as List<dynamic>;

    for (final item in data) {
      final map = item as Map<String, dynamic>;
      await storeStructuredData(
        map['id'] as String,
        map['source'] as String,
        map['data'],
      );
    }
  } catch (e) {
    throw FormatException('Invalid JSON data: $e');
  }
}