web_socket_channel throwing Unsupported operation: Platform._version - flutter

How can I use the websocket package with dart web? I have the following code
import 'package:projectname/data/chat/chat_message.dart';
import 'package:projectname/data/chat/chat_provider.dart';
import 'package:web_socket_channel/io.dart';
import '../path.dart';
class MockChatProvider implements ChatProvider {
#override
IOWebSocketChannel connect() {
return IOWebSocketChannel.connect(Uri.parse(Path.joinChat));
}
#override
sendChatMessage(IOWebSocketChannel channel, ChatMessage message) {
channel.sink.add(message.toJson());
}
}
But when I try to connect I get the following error
Unsupported operation: Platform._version
The package does say it supports web. What am I doing wrong?

Just use the package package:web_socket_channel/web_socket_channel.dart
TLDR;
I think you should use this...
import 'package:web_socket_channel/web_socket_channel.dart';
...instead of...
import 'package:web_socket_channel/io.dart';
and use it as
WebSocketChannel connect() {
...
return WebSocketChannel.connect(Uri.parse(Path.joinChat));
...
sendChatMessage(WebSocketChannel channel, ChatMessage message) {
Note: I have not tried and tested it!
I think the problem is that you found the right package, but you're using directly IOWebSocketChannel. That only works on places where dart:io is available. There's another class in that package, HtmlWebSocketChannel that only works on the web.

I think the issue is that you are importing package:web_socket_channel/io.dart for web and you should instead import package:web_socket_channel/web_socket_channel.dart.
Related GitHub issue on package's repo: https://github.com/dart-lang/web_socket_channel/issues/159

Related

Is it possible to make web only injection with Injectable?

I'm using Injectable and I have two classes. WebImageDownloader and MobileImageDownloader. Both are implementing ImageDownloader which is an abstract class. I'm registering web and mobile image downloaders as ImageDownloader with Injectable, one registered under web, one under mobile environment.
But since WebImageDownloader has dart:js and dart:html imports, I can't run my code. Because these libraries are not found.
I see on some answers that people do conditional imports between dart:io and dart:html but it doesn't work for me.
Also, the question is under issues for Injectable.
import 'MobileFileDownloader.dart'
if (dart.library.html) 'WebFileDownloadService.dart';
abstract class NativeFileDownloader {
void downloadFile(List<int> bytes, String downloadName);
factory NativeFileDownloader() =>getDownloader();
}
import 'NativeFileDownloader.dart';
class WebFileDownloadService implements NativeFileDownloader{
void downloadFile(List<int> bytes, String downloadName) {
// Encode our file in base64
final _base64 = base64Encode(bytes);
// Create the link with the file
final anchor = AnchorElement(href: 'data:application/octet-stream;base64,$_base64')
..target = 'blank'
..setAttribute('download', downloadName);
// add the name
//anchor.download = downloadName;
// trigger download
document.body?.append(anchor);
anchor.click();
anchor.remove();
return;
}
}
NativeFileDownloader getDownloader()=>WebFileDownloadService();
import 'package:flovo/Services/FileDownloadService/Native/NativeFileDownloader.dart';
class MobileFileDownloader implements NativeFileDownloader{
#override
void downloadFile(List<int> bytes, String downloadName) {
}
}
NativeFileDownloader getDownloader()=>MobileFileDownloader();
use => NativeFileDownloader().downloadFile(list, 'image.jpeg');

Eclipse JUnit 5 SecruityException when running Tests

I think I may be the only one experiencing this issue.
I, today, updated my eclipse install to version 2020-03 (4.15.0). I am also attempting to write a very simple JUnit 5 test for a new method I'm working on.
When I run my test, right now just a basic stub, I get the following error:
java.lang.SecurityException: class "org.junit.platform.commons.PreconditionViolationException"'s signer information does not match signer information of other classes in the same package
at java.base/java.lang.ClassLoader.checkCerts(ClassLoader.java:1150)
at java.base/java.lang.ClassLoader.preDefineClass(ClassLoader.java:905)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1014)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:821)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:719)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:642)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:600)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.createUnfilteredTest(JUnit5TestLoader.java:75)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.createTest(JUnit5TestLoader.java:66)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.loadTests(JUnit5TestLoader.java:53)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:526)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
I also see the following dialog
My run Configuration is:
I've tried all major junit-jupiter (aggregator) releases back to 5.5.0 all resulting in the same issue.
I've tried this solution. However, that question deals with a class not found issue. I also tried that same solution using using junit-platform-commons version 1.6.1. no change.
However, I can run maven configuration with -Dtest=DeaFileListTest test the the tests run.
My test case is simple, I instantiate an object that has the method I want to test and then my test.
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.not;
import java.io.IOException;
import java.util.List;
import javax.ws.rs.core.Response;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import com.mfgweb.FileRepo;
class DeaFileListTest {
private static FileRepo filerepo;
private static Response response;
#BeforeAll
static void setUpBeforeClass() throws Exception {
filerepo = new FileRepo();
response = filerepo.getDeaFiles();
}
#AfterAll
static void tearDownAfterClass() throws Exception {
response = null;
filerepo = null;
}
#Test
public void deaFileListIsNotEmptyTest() throws IOException {
#SuppressWarnings ( "unchecked" )
List< String > files = ( List< String > )response.getEntity();
assertThat( files, not( empty() ) );
}
}
So I am curious why I'm receiving the Security Exception when I run the test in eclipse, yet Maven seems to execute them fine.

how to call dart package method from Flutter app

I am trying to create a dart package and use it in my few flutter projects. The package is not public it’s a private package. I am trying to understand how to create a package with working on an example. The documentation wasn’t so difficult, but it confuses me, because I am new to this type project.
So, I create private dart package project. My package name is socket_conn. The socket_conn.dart has single export line as export 'src/socket_conn_base.dart'; My socket_conn_base.dart has 1 class and 1 Future method.
My intention is to import this package to my flutter app, send data to EncrptedSocketCommunication and wait EncrptedSocketCommunication returns data from getQuery. It didn’t work.
I am not sure if I am doing right but calling EncrptedSocketCommunication how do I fire getQuery method, so it goes another class under my src folder (getQueryA100) and gets the data. The getQueryA100 has a working and tested code. But when I import this package to my flutter app
İt doesn’t call Future>> getQuery.
My question is how to call dart package method from Flutter app?
import 'csbins_socket/getQueryA100.dart';
List<List<dynamic>> _returnData;
class EncrptedSocketCommunication {
String connectionText;
String queryText;
String parameterText;
EncrptedSocketCommunication(
this.connectionText,
this.queryText,
this.parameterText
);
Future<List<List<dynamic>>> getQuery(String connectionText, String queryText, String parameterText) async {
switch (queryText){
case "QA100": {
_returnData = await getQueryA100(queryText, parameterText);
return _returnData;
}
break;
}
}
}

Flutter - How get context from MockRepository?

I'm making an example with flutter and I've come across a question mark. In my project I have implemented dependency injection and I have two classes to get data one for production and testing with local data (Mock). The problem is that the local data I have stored in a json file and when I implement the functionality "fetchProducts" I do not know how to get the Context to load the json... I hope you can help me, thanks.
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:graphqllapp/data/product_data.dart';
import 'package:path/path.dart';
class MockProductRepository implements ProductRepository {
#override
Future<List<Product>> fetchProducts() async {
// TODO: implement fetchUsers
String data = await
DefaultAssetBundle.of(????).loadString("mockdata/data.json");
var jsonResult = json.decode(data);
return new Future.value(products);
}
}
You can instead use rootBundle which is the default value of DefaultAssetBundle
rootBundle.loadString("mockdata/data.json");

Dart: Target of URI does not exist

My Dart app project structure:
myapp/
pubspec.yaml
pubspec.lock
asset/
...assets
build/
packages/
web/
lookups/
AjaxLookups.dart
requests/
RESTClient.dart
AjaxRESTClient.dart
The AjaxLookups file:
library myapp;
abstract class AjaxLookups {
static final String BASE_URL = "/myapp";
static final String DO_S0METHING_SERVICE_URL = BASE_URL + "/doSomething";
}
The RESTClient file:
library myapp;
typedef void Callback(String json);
abstract class RESTClient {
void get(String url, Callback onFail, Callback onSuccess);
void post(String url, String dataJSON, Callback onFail, Callback onSuccess);
}
The AjaxRESTClient file:
library myapp;
import "RESTClient.dart";
import "../lookups/AjaxLookups.dart";
import "dart:html";
import "dart:convert" show JSON;
class AjaxRESTClient implements RESTClient, AjaxLookups {
// ...
}
Above, the import statement for AjaxLookups is causing a compiler error:
Target of URI does not exist: '../request/AjaxLookups.dart'
Why am I getting this? Why can't Dart find ../request/AjaxLookups.dart? What do I need to do to fix it?
It looks like the file AjaxLookups.dart is in the lookups folder, so your import should be:
import "../lookups/AjaxLookups.dart";
I figured it out. I was declaring a new myapp library inside each source file. Instead I added a main/driver Dart file, declared a library in it called myapp, and then changed all the other source files to be part of myapp;.