I am trying to use dart-io to read and write file. I am getting below exception.
Uncaught Error: Unsupported operation: _Namespace
at Object.dart.throw (dart_sdk.js:4537)
at Function.get _namespacePointer [as _namespacePointer] (dart_sdk.js:49364)
at Function._namespacePointer (dart_sdk.js:47224)
at Function._dispatchWithNamespace (dart_sdk.js:47227)
at io._File.new.open (dart_sdk.js:47342)
at new io._FileStreamConsumer.new (dart_sdk.js:47186)
at io._File.new.openWrite (dart_sdk.js:47468)
at refresh (main.dart:15)
at refresh.next (<anonymous>)
at runBody (dart_sdk.js:22264)
at Object.async.async (dart_sdk.js:22292)
at main.refresh (main.dart:13)
at Object.dart._checkAndCall (dart_sdk.js:4731)
at Object.dart.dcall (dart_sdk.js:4736)
Code I used final output = io.File('output.txt').openWrite(); output.write(output_data);
Could you please help me in fixing it.
Or any other way to read and write files using dart in a web application.
if (kIsWeb) {
image = Image.network(pickedFile.path);
} else {
image = Image.file(File(pickedFile.path));
}
The answer is in the documentation https://pub.dev/packages/image_picker
dart:io
can not be used in web projects. dart:html
has some limited file API (limited to what the browser allows). Dart in the browser can not do anything the browser does not provide.
(Not sure why Günter didn't put this as an answer...)
use https://pub.dev/packages/image_picker library.
in dart file :-
import 'dart:io';
for pick the image use following code :-
_imgFromGallery() async {
final temp = (await ImagePicker().
getImage(source:ImageSource.camera,imageQuality:
80));
imageForSendToAPI = await temp.readAsBytes();
setState(() {});
}
This method will take image from desktop.
here imageForSendToAPI will give you uint8list,which you can pass to API.
for showing the selected file use :
Image.memory(imageForSendToAPI)
for sending to API don't use multipart, just pass like below:-
data = {
"profile_image":imageForsendToAPI.toString();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With