JKRhb / dtls2

A DTLS library for Dart based on OpenSSL.
MIT License
2 stars 0 forks source link

Can not build on web when this package is referenced. #117

Open esDotDev opened 1 month ago

esDotDev commented 1 month ago

We have an app that is targeting iOS and Android, but it runs in test mode in the browser. When running in the browser we would use mock data and never call the DTLS API directly. Like:

onPress: (){
  if(kIsWeb){
    // use mock data
  } else {
    // call native APIs
  }
}

Currently when trying to run on web, if the package is imported at all, we get an error and can not build the application:

/C:/Users/Shawn/AppData/Local/Pub/Cache/hosted/pub.dev/dtls2-0.16.0/lib/src/dtls_client.dart:6:8: Error: Dart library 'dart:ffi' is not available on this platform.
import "dart:ffi";
       ^

I know we could maybe do some sort of conditional compilation on the app side, but it would be a much nicer dev UX if the package could handle it. Is this even possible?

JKRhb commented 1 month ago

Hi @esDotDev, thank you for opening this issue! If I see it correctly, conditional imports are not that well supported by Dart, but according to this article (and others), you can do something like this:

import "something.dart" if (dart.library.io) "other.dart"

Maybe you can try to integrate that into your app already, I will also have a look into adding this to the package so that the dev experience will be improved :)

esDotDev commented 1 month ago

Thanks for looking at this! Yes I think it is probably possible to do with conditional compilation downstream, but it's a bit involved as you would need to make dummy classes for every public datatype and method.

We're actually relying on a package that depends on yours (flutter_hue), so it's maybe even trickier to deal with conditionally, as flutter_hue has many types we consume on the client side.

I'm not sure that there is anything you can do here, but just thought we'd ask :)