Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsupported operation: _Namespace while using dart io on web

Tags:

io

web

dart

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.
like image 826
Sainath Sarma Avatar asked Feb 25 '19 07:02

Sainath Sarma


3 Answers

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

like image 147
Felix Garcia Avatar answered Nov 10 '22 08:11

Felix Garcia


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...)

like image 20
Kevin Moore Avatar answered Nov 10 '22 09:11

Kevin Moore


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();
}
like image 4
Mohit Modh Avatar answered Nov 10 '22 08:11

Mohit Modh