Dart JS Interop problem

I’m converting one of my packages from dart:html to using package web and js interop in preparation for wasm compilation and encountering the following error -

in the function

Future update(
String key, JsonObjectLite document, String revision) {

I do this -

if (revision.isNotEmpty) { …

and get this error when running in the browser -

NoSuchMethodError: method not found: 'get$isNotEmpty' (revision.get$isNotEmpty is not a function)

Seems to suggest that isNotEmpty is not present for the String type.

The value of revision is “”.

What am I missing?

How is the update function called from the browser? Show some sample code with the setup?

The problem affects all usages of isNotEmpty in the entire file, not just this function.

I’ve found if I change the code to -

if (revision.toJS.isDefinedAndNotNull) { …

it works.

Is this expected, seems odd, I feel as though I’m missing a helper package or something.

This looks like JS string was not properly converted to Dart String on the boundary. You need to post a more complete code for us to be able to help you. This can be a mistake in your code (e.g. you specified weak signature somewhere and then just did a cast from a JS value to a Dart String) or there can be a bug in the JS interop implementation. In either case we will need to see more code to understand it.

2 Likes

Yes OK,

Please pull this repo and switch to branch issue33.

I’m running a unit test file as below -

dart test test/lib/sporran_test.dart -p chrome

The first failure point is lib/src/sporran_database.dart line 553

This codebase is very old by modern dart standards as you will see, it could do with a rewrite to use async/await(wasn’t available 10 years ago) but I don’t see why this would affect anything.

Sorry for the work here but I don’t think posting endless code snippets will help.

I have tried that and I get all kinds of exceptions but not the get$isNotEmpty one you have mentioned in the issue. I have tried both stable and main branches of Dart.

I also see that the code is changed compared to what you describe (e.g. you have revision.toJS.isDefinedAndNotNull instead of revision.isNotEmpty in the code).

Yep, you’ll need a running couchdb instance with CORS configuration set to run the tests, for some reason I had just assumed you would be inspecting the generated code only, my bad.

However, a combination of your comment above about how the function is being called and my observation that this code is old has lead to a resolution.

The code base uses dynamic far too much, looks as though this seems to cause type elision in some circumstances when compiling to JS. The fix of course is to get rid of these calls, works OK when dynamic is replaced with the actual type which only improves things to boot.