how to call dart package method from Flutter app - class

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;
}
}
}

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');

web_socket_channel throwing Unsupported operation: Platform._version

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

How to set a custom property for conditional import in Dart?

Dart allows to use the standard library names for conditional import/export, like this:
export 'src/hw_none.dart' // Stub implementation
if (dart.library.io) 'src/hw_io.dart' // dart:io implementation
if (dart.library.html) 'src/hw_html.dart'; // dart:html implementation
Is it possible to define a custom property/condition? For example, pass it when compiling.
I have a project that I would like to split into two variants: Flutter variant and pure Dart variant. The choice of the variant depends at compile time, and the necessary implements of abstract classes defines at compile time.
Custom properties where an experimental feature in Dart 1. With Dart 2 there is no longer any support for user defined custom conditions in compile time.
Here is the discussion referencing your question.
All you can do is, switch between implementations on run time:
abstract class SomeFactory {
String get message;
factory SomeFactory() {
if(Platform.environment['SOME_VAR'] != null)
return new _SomeImplementation();
return new _SomeOtherImplementation();
}
}
class _SomeImplementation implements SomeFactory {
#override
String get message => 'SomeImplementation';
}
class _SomeOtherImplementation implements SomeFactory {
#override
String get message => "OtherImplementation";
}
Check this blog entry for more details.

de.hybris.eventtracking.model.events.AbstractTrackingEvent cannot be resolved to a type

I just finished configuring hybris and tried to set up the eclipse project. As per guidelines in the wiki.hybris, I imported all the extensions into the eclipse project. When I try into build and clean, I get more than 3000 compiler errors. One of the errors is the class AbstractTrackingEvent cannot be resolved to a type. I looked for the particular class in the project folder. I could not find the folder events under de.hybris.eventtracking.model, which is the cause of the issue.
Am I missing anything while importing the project? There are many such type of issues in my eclipse project. Please let me know how to fix it. I have attached the screenshot for reference.
Note: I am using hybris-commerce-suite 5.7.0.8
As requested, I am adding the source code.
package de.hybris.eventtracking.services.populators;
import de.hybris.eventtracking.model.events.AbstractTrackingEvent;
import de.hybris.eventtracking.services.constants.TrackingEventJsonFields;
import de.hybris.platform.servicelayer.dto.converter.ConversionException;
import java.io.IOException;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* #author stevo.slavic
*
*/
public abstract class AbstractTrackingEventGenericPopulator implements
GenericPopulator<Map<String, Object>, AbstractTrackingEvent>
{
private final ObjectMapper mapper;
public AbstractTrackingEventGenericPopulator(final ObjectMapper mapper)
{
this.mapper = mapper;
}
public ObjectMapper getMapper()
{
return mapper;
}
protected Map<String, Object> getPageScopedCvar(final Map<String, Object> trackingEventData)
{
final String cvar = (String) trackingEventData.get(TrackingEventJsonFields.COMMON_CVAR_PAGE.getKey());
Map<String, Object> customVariablesPageScoped = null;
if (StringUtils.isNotBlank(cvar))
{
try
{
customVariablesPageScoped = getMapper().readValue(cvar, Map.class);
}
catch (final IOException e)
{
throw new ConversionException("Error extracting custom page scoped variables from: " + cvar, e);
}
}
return customVariablesPageScoped;
}
}
"As per guidelines in the wiki.hybris, I imported all the extensions into the eclipse project."
I don't think the guidelines tell you this. Basically, you want the projects loaded to be the same as those defined in your localextensions.xml and their dependencies. The reason you can't see those is they are not built.
Ensure you have run 'ant build' successfully, refresh the platform project, remove any extensions from your workspace that are not needed for your project, and clean and build in eclipse.
Make sure you have provided the project dependencies in each project by checking their individual extensioninfo.xml files as shown in below image.
Also sometimes dependent libraries are not imported properly check for those too.

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