saveJsonExport function

Future<String> saveJsonExport(
  1. Map<String, dynamic> data
)

Save JSON data via the native "Save to" dialog so the user can choose the destination (e.g. Downloads, iCloud Drive, Google Drive). Returns the chosen path, or throws if the user cancels.

Implementation

Future<String> saveJsonExport(Map<String, dynamic> data) async {
  final jsonStr = jsonEncode(data);
  final dateStr = DateTime.now().toIso8601String().substring(0, 10);
  final filename = 'dutato-export-$dateStr.json';
  final bytes = utf8.encode(jsonStr);

  final path = await FilePicker.platform.saveFile(
    dialogTitle: 'Save data export',
    fileName: filename,
    type: FileType.custom,
    allowedExtensions: ['json'],
    bytes: bytes,
  );

  if (path == null) {
    throw StateError('Export cancelled');
  }
  return path;
}