Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions lib/src/widgets/button_file_saver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';

import 'package:file_picker/file_picker.dart';
import 'package:universal_html/html.dart' as html;
// import 'package:universal_html/html.dart' as html;
import 'package:universal_web/js_interop.dart';
import 'package:universal_web/web.dart' as web;

/// JSON encoder with indentation for pretty printing.

Expand Down Expand Up @@ -83,14 +85,30 @@ Future<void> downloadDataForWeb(
filename = '$filename.json';
}
final bytes = utf8.encode(jsonContent);
final blob = html.Blob([bytes], 'application/json');
final url = html.Url.createObjectUrlFromBlob(blob);
// final blob = html.Blob([bytes], 'application/json');
// final url = html.Url.createObjectUrlFromBlob(blob);

html.AnchorElement(href: url)
..setAttribute('download', filename)
..click();
// html.AnchorElement(href: url)
// ..setAttribute('download', filename)
// ..click();

html.Url.revokeObjectUrl(url);
// html.Url.revokeObjectUrl(url);

final blob = web.Blob(
[bytes.buffer.toJS].toJS,
web.BlobPropertyBag(type: 'application/json'),
);
final url = web.URL.createObjectURL(blob);

final anchor = web.HTMLAnchorElement()
..href = url
..download = filename;

web.document.body?.append(anchor);
anchor.click();
anchor.remove();

web.URL.revokeObjectURL(url);

messenger.showSnackBar(
SnackBar(content: Text('Data downloaded as $filename')),
Expand Down
5 changes: 3 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ dependencies:
media_kit_libs_video: ^1.0.7
media_kit_video: ^2.0.1
path: ^1.9.1
universal_html: ^2.3.0
url_launcher: ^6.3.2
path_provider: ^2.1.5
# universal_html: ^2.3.0
universal_web: ^1.1.1+1
url_launcher: ^6.3.2

dev_dependencies:
custom_lint: ^0.8.1
Expand Down