generateFile method
Generates a JSON file representation of the event for sharing or local persistence.
The file name will be derived from the event id and timestamp unless
fileName is provided. The file will be created in directory if supplied,
otherwise the platform's temporary directory is used.
Implementation
@protected
Future<File> generateFile(
ReportEvent event, {
String? fileName,
Directory? directory,
}) async {
final dir = directory ?? await getTemporaryDirectory();
final name = (fileName?.trim().isNotEmpty ?? false)
? _sanitizeFileComponent(fileName!.trim())
: _defaultFileName(event);
final path = '${dir.path}${Platform.pathSeparator}$name';
final file = File(path);
// Use the event's own JSON encoder to preserve consistent shape/ordering.
final contents = event.toJson();
// Ensure parent directory exists.
await file.parent.create(recursive: true);
return file.writeAsString(contents, flush: true);
}