How to ignore package when building flutter project for web?
Maybe you should guard your usages of libserialport
with the kIsWeb
predicate like following:
if(!kIsWeb){
// libserialport code execution here
}
I searched a lot as well and didn't find a way you can do that, I think this should be handled by the package itself not the package's users like in path_provider
for instance.
As a workaround I have created a dummy libserialport
's SerialPort
class for web only as follows:
dummy_serialport.dart
:
class SerialPort {
final String name;
static List<String> availablePorts = ['dummy'];
static SerialPortError? lastError;
SerialPort(this.name);
bool openReadWrite() {
return false;
}
}
class SerialPortError {}
// add more properties and functions as needed
main.dart
:
import 'package:libserialport/libserialport.dart'
if (dart.library.html) './dummy_serialport.dart'
if (dart.library.io) 'package:libserialport/libserialport.dart';
....
if (!kIsWeb) {
final name = SerialPort.availablePorts.first;
final port = SerialPort(name);
if (!port.openReadWrite()) {
print(SerialPort.lastError);
exit(-1);
}
}
....
....
It's bad, I know :( but it works! maybe you can contact the package author to get more insight and if opening a PR where the interfaces are separated from the FFI implementation so that importing the classes wouldn't break web or something.
xerotolerant
I'm the Technical Director at Raiora. I learn because I must.
Updated on January 02, 2023Comments
-
xerotolerant over 1 year
I have a project which uses
flutter_libserialport
library on macOS. I am modifying it to work on web however this library does not work on web.I am building a web implementation using
navigator.serial
in javascript which works fine.However when I attempt to build the project for web I get the following error
/opt/homebrew/Caskroom/flutter/2.2.3/flutter/.pub-cache/hosted/pub.dartlang.org/libserialport-0.2.0+3/lib/src/config.dart:25:8: Error: Not found: 'dart:ffi' import 'dart:ffi' as ffi;
This makes sense since FFI is not available on web.
But I don't even need the
libserialport
library on web any way.How can I get flutter to ignore it?
I tried this however it doesn't contain information on how to exclude a package.
It also does not contain information on how to ignore it specifically for web. It seems to just ignore it in general.
-
Diwyansh over 2 yearscheck this stackoverflow.com/questions/58710226/….
-
xerotolerant over 2 yearsI found it a few minutes ago. it might be what I need.
-
Diwyansh over 2 yearsokay good just try it
-
xerotolerant over 2 years@Diwyansh I was successful thanks.
-
Diwyansh over 2 yearsPleased to help you...
-
-
xerotolerant over 2 yearskIsWeb is a runtime variable. The issue I'm having is at build time. I want to exclude it from the build entirely
-
Ayman Barghout over 2 yearsYea, you are right. It's really weird that when I searched I found nothing but I came up with a really bad workaround I hope it helps, for now, I'll update my answer.