Closed Sylphide closed 3 years ago
Having same problem on universal_html: ^2.0.7:
import "package:universal_html/html.dart" as html; html.Worker myWorker; myWorker = new html.Worker('upload_worker.js'); myWorker.onMessage.listen((e) { print("Received message from web worker ${e.data}"); }); myWorker.onError.listen((e) { print("Received error from web worker ${e.toString()}"); });
Building Windows application... lib/drawer.dart(94,10): error G3163D78C: 'Worker' isn't a type. [C:\flutter\musicplayer\build\windows\flutter\flutter_assemble.vcxproj] lib/drawer.dart(97,25): error GB1B8BC88: Method not found: 'Worker'. [C:\flutter\musicplayer\build\windows\flutter\flutter_assemble.vcxproj]
I'm going to just post my complete function where I use your library I wrote other day so it hopefully helps you:
open_file3(BuildContext context) async { html.Worker myWorker; //html.File file; var url = 'https://X.com:8000/uploads/'; myWorker = new html.Worker('upload_worker.js'); myWorker.onMessage.listen((e) { print("Received message from web worker ${e.data}"); }); myWorker.onError.listen((e) { print("Received error from web worker ${e.toString()}"); });
html.InputElement uploadInput = html.FileUploadInputElement();
uploadInput.multiple = true;
uploadInput.click(); //this opens file explorer on their computer
//suppose I could use a different web worker for each file but fuckit
uploadInput.onChange.listen((e) {
uploadInput.files.forEach((file) { //loop the uploadInput.files array for the files user selected
//file = uploadInput.files.first;
print("File Selected was ${file}");
myWorker.postMessage({"file": file, "url": url});
});
});
}
For calling it on WEB vs mobile/desktop I just use: GetPlatform.isWeb ? open_file3(context) : open_file(context);
with GetX framework, hope this helps.
For information, I tried to isolate my Worker usage in a specific file and tried using a deferred loading only on web but it still doesn't work.
Found a workaround:
import 'lib_mobile.dart'
if(dart.library.js) 'lib_web.dart';
Hi! Thanks for the issue report! I fixed this in version 2.0.8.
Works perfectly, thank-you.
Hi,
I am currently using version 1.2.4 of universal_html.
I tried using the Worker class in order to execute a js script to upload a large file to an external server. It works fine in practice but it broke my unit test. I guess since the tests doesn't execute in the same environment as a web browser, the Worker class isn't defined. My tests can't compile and I get this kind of errors:
Should I upgrade universal_html to 2.X (hence my flutter version which is still in a pre release version)?
Thanks