Dart plugin dynamic linking - dart

I am having trouble utilising the libserialport.dart package. I have put a libserialport.so in the root of the project. When trying to run an application I get the following error:
Unhandled exception: Invalid argument(s): Failed to load dynamic library 'libserialport.so': libserialport.so: cannot open shared object file: No such file or directory
This tells me that the package is looking for the file somwhere else - but where?
The original library links the library this way, which results in it not finding the library:
LibSerialPort? _dylib;
LibSerialPort get dylib {
return _dylib ??= LibSerialPort(ffi.DynamicLibrary.open(
resolveDylibPath(
'serialport',
dartDefine: 'LIBSERIALPORT_PATH',
environmentVariable: 'LIBSERIALPORT_PATH',
),
));
}
If I replicate the plugin locally, but changing the linking as such, the library works as expected:
var libraryPath =
path.join(Directory.current.path, 'libserialport.so');
LibSerialPort? _dylib;
LibSerialPort get dylib {
return _dylib ??= LibSerialPort(ffi.DynamicLibrary.open(libraryPath));
}
The question is: where to put the .so file so it would work with the original verison? Where does resolveDylibPath() link to?
If possible I would like to avoid using my modified version as that brings license implications I am not entirely sure how to deal with.

Apparently the function looks for the path in the LIBSERIALPORT_PATH enviroment variable. Setting it to '.' made it work!
In the terminal:
export LIBSERIALPORT_PATH=.

Related

Is there any way of loading local resource files in dart? (not flutter)

I want to load the bytes of a file into a variable while testing my flutter application.
I can't use the assets directory as those are bundled with the app and require WidgetsFlutterBinding.ensureInitialized();
I tried searching the file manually with the path package, but this did not seem to work and was rather hacky. That is why i'm searching for a more official approach.
I was thinking way to complicated ...
As Chuck Batson commented, you can just use the path from the projects root for passing it into the (dart:io) File:
File loadResource(String relativePath) {
final filePath = path.join("test", "resources", relativePath);
return File(filePath);
}
(Notice: The above code makes use of the path package for constructing a file path.)

Adding native swift code to NativeScript application

I'm trying to add native swift code to my NativeScript app. According to these instructions in the documentation I can just add a swift source file to App_Resources/iOS/src/ and then use any publicly exposed classes directly in my TypeScript code.
Unfortunately this just doesn't work. I'll just get Cannot find name 'TestClass' and that's it.
Steps to reproduce:
Get a fresh NS project with ios tns create my-app-name --template tns-template-blank-ts
Update: I actually created the App with vue init nativescript-vue/vue-cli-template testapp. That seems to have caused the problems.
Add a TestClass.swift to App_Resources/iOS/src/
import Foundation
public class TestClass: NSObject {
#objc public func echo(param: String) -> String {
return param
}
}
Instantiate it in any TypeScript source file let instance = new TestClass()
Do tns debug ios
Compilation will fail with Cannot find name 'TestClass'
I have also tried generating TypeScript typings with TNS_TYPESCRIPT_DECLARATIONS_PATH="$(pwd)/typings" tns build ios or or just delcaring it as any with declare let KeyCommander: any; to eliminate the possibility that this is a TS related problem. The first approach doesn't generate any typings for my custom class so the TypeScript code will still not compile. The second approach let's the TS code compile but crashes on execution with JS ERROR ReferenceError: Can't find variable: TestClass.
I have also verified that the swift file is indeed getting compiled by inserting a syntax error which will crash the build process.
My NativeScript version is 6.4.0.
What am I missing?
Update: I just realized I actually created the App with vue init nativescript-vue/vue-cli-template testapp. I verified that as mentioned Tyler Blake's answer in an app created with the tns cli the described process actually works. In an app I just freshly created with vue init it doesn't, the objc!nsswiftsupport.d.ts is not being generated.
The question now is: What's causing the difference?
I followed your steps and I was able to get the typings to generate in objc!nsswiftsupport.d.ts. After you generate typings do you have that file with these contents?
declare class TestClass extends NSObject {
static alloc(): TestClass; // inherited from NSObject
static new(): TestClass; // inherited from NSObject
echoWithParam(param: string): string;
}
This shows that NS is able to pick up the Swift code.
All you need to do now is add tns-platform-declarations then in the references.d.ts file, add a line that points to the objc!nsswiftsupport.d.ts file. Then you'll get intellisense in your TS code.
Something like this:
/// <reference path="./typings/objc!nsswiftsupport.d.ts" />
Hope this helps!
I was able to solve the problem by inspecting the differences between the templates created with tns-cli and vue init. The difference is that the vue init template ships with an outdated version of the nativescript platform. You can just simply change
"tns-ios": {
"version": "6.0.1"
}
to version 6.4.0 (which the version the tns-cli template comes with) and then the process will work as described in the documentation.

static final object changes identity

I have a browser application written in Dart. I noticed a strange error appearing where my StageXL ResourceManager was missing the resources that it previously had. After debugging the program for a while I ended up with this situation:
In global.dart:
class Global {
static final ResourceManager resourceManager = new ResourceManager();
}
In main function:
var resources = Global.resourceManager;
resources.addBitmapData("Player", "images/player_base.png");
await resources.load();
print("in main: ${identityHashCode(Global.resourceManager)} = "
" ${Global.resourceManager.resources}, isolate: ${identityHashCode(
Isolate.current)}");
In another function where I need to access the resource afterwards:
print("elsewhere: ${identityHashCode(Global.resourceManager)} = "
" ${Global.resourceManager.resources}, isolate: ${identityHashCode(
Isolate.current)}");
Expected output (identityHashCodes match and so do the object contents):
in main: 12345678 = [ResourceManagerResource [kind=BitmapData, name=Player,
url = images/player_base.png]], isolate: 09876543
elsewhere: 12345678 = [ResourceManagerResource [kind=BitmapData,
name=Player, url = images/player_base.png]], isolate: 09876543
Actual output (note the identityHashCode mismatch):
in main: 516570559 = [ResourceManagerResource
[kind=BitmapData, name=Player, url = images/player_base.png]],
isolate: 843028171
elsewhere: 419835243 = [], isolate: 843028171
I thought this may have something to do with running in a different isolate (not familiar with them) but as you can see, the current isolates identityHashCodes match.
That is surprising. My best guess is that you are importing the same library twice, using different URIs. The fact that one of your files is a "main" file supports this, since its a common mistake to specify the main file on the command line as a file and have it import a package library using a relative reference.
Is your "main" file in a package lib directory, and does it import the resource file using a relative path? If so, try changing that import to a package:packageName/thepath URI instead and see if it changes anything.
(My personal recommendation is to never have a Dart library URL that contains lib, whether in an import/export or on the command line. Always use a package: URI in that case instead.)

VCLua library fault to load

I have downloaded VCLua library here. I created new file named program.lua and typed there code that is in tutorial:
require "vcl"
mainForm = VCL.Form("mainForm")
mainForm.Caption = "My first VCLua application"
mainForm._= { position="podesktopcenter", height=400, width=600}
mainForm.onclosequery = "onCloseQueryEventHandler"
function onCloseQueryEventHandler(Sender)
return true -- the form can be closed
end
mainMenu = VCL.MainMenu(mainForm,"mainMenu")
mainMenu:LoadFromTable({
{name="mmfile", caption="&File",
submenu={
{name="mmOpen", caption="Open...", onclick="onMenuOpenClick", shortcut="Ctrl+O"},
{caption="-",},
{caption="Exit", onclick="onMenuExitClick", shortcut="Alt+F4"},
}
},
{name="mmhelp", caption="&Help", RightJustify=true,
submenu = {
{caption="Help", shortcut="F1", checked=true},
{caption="-",},
}
}
})
mainMenu:Find("mmhelp"):Add("mmAbout")._= {caption="About", onclick="onMenuAboutClick", enabled=false}
function onMenuExitClick()
mainForm:Close()
end
mainToolbar = VCL.ToolBar(mainForm,"mainToolbar")
mainToolbar:LoadFromTable({
{name="mtbOpen", onclick="onMenuOpenClick"},
{name="mtbExit", onclick="onMenuExitClick"},
})
mainToolbar:Find("mtbOpen").enabled=false
mainForm:ShowModal()
mainForm:Free()
But when i want to run it (i have both vcl.dll and vcl.so files in directory where lua interpreter is), it writes lua: error loading module 'vcl' from file 'C:\Users\Admin\Desktop\Programs\lua\vcl.dll':. Stack trace dont say anything useful. So can anyone tell me, how to solve my problem?
Dynamic libraries for Lua are usually compiled against Lua DLLs; you may get this error in a case when the dynamic library can't find the Lua DLLs it's linked against. You can see why/how it fails using a tool like dependency walker, which can show you both what other DLLs your library depends on and also what run-time error you get when you load it (you can use "Profile" mode to see that).

importing dart code from other projects

** This question is edited and cleaned up some **
I have two projects and I want to use code from one in the other; I seem to be having trouble putting the code in the right directory structure to make the import statements work.
Both projects are created and managed exclusively from the Dart Editor on a Mac, if that makes any differences.
Project Directory Structures
Project 1: a command line app which contains the code I want to share in the following directory structure:
/dart/command_line_app
/lib
shared_library.dart
/bin
command_line_app.dart
Project 2: a web app which wants to import the code in shared_libary.dart
/dart/web_application
/packages
/web
web_application.dart
In the file shared_libary.dart, I declare it to be a library can create a simple class that provides output when instantiated:
library shared_library;
class ShareMe
{
ShareMe()
{
print("Hello, ShareMe");
}
}
This compiles, and works inside the command_line project: command_line_app.dart has the following:
import 'package:command_line_app/shared_library.dart';
void main() {
ShareMe shareMe = new ShareMe();
print("Hello, World!");
}
This imports the code runs, printing both "Hello Share Me," and Hello World.
THE PROBLEM
I want to instantiate the ShareMe class inside web_application.dart. I'd thought I could do that by putting in the same import statement I put in my command_line code:
import 'package:command_line_app/shared_library.dart';
But, when I put the same import into the web_appliation, it gets the error
Target of URI does not exist 'package:command_line_app/shared_library.dart'
Other Things I've Tried
I was certain I'd solved the problem when I cntrl-Clicked properties on Web_application and selected Project References.
It brings up a window allowing me to select command_line_app with a check box, but when I do, I get an error:
Could not set the project description for 'web_application' because the project description file (.project) is out of sync with the file system.
Whatever that means.
When I cntrl-click the underlined error and try Quick Fix it offers me "resolve dependencies" which sounds promising, but after a few seconds, it comes back and informs me that
Pub get failed, [1] Resolving dependencies... (15.3s)
Could not find package command_line_app at https://pub.dartlang.org.
Depended on by:
- web_application 0.0.0
I hope this is clear-er and gives a better insight into both what I'm trying to do and what I'm missing.
EDIT
you need to add
dependencies:
command_line_app:
path: ../command_line_app
to your dependencies in web_application/pubspec.yaml.
EDIT END
When you want to make code reusable in different packages, you should put that code into the lib directory of that package and import it using import 'package:mypackage/myfile.dart';.
Another problem you may face is, that browser applications can't import packages that have a dart:io dependency. If you want to reuse code between command line and browser applications you should move them into the lib directory of another package my_shared_code where you put only code that doesn't depend on dart:io (for example some entity classes) and import this code from both app packages (browser and command line).

Resources