I recently wanted to implement a solution that needed to run on both desktop and web that uses Serial port communication. Since there are no out of the box libraries that support both of those platforms. i am using
Then i knew that i needed to selectively import the libraries based on the platform i am compiling the code for. So i found another Stackoverflow thread that explained how to do that. Platform Dependent imports solution
I read through that answer and implemented the same thing. For windows the code compiles successfully and behaves as i expected. But the web doesn't compile and throws an error during compilation.
i will shared the code files below too.
abstract class
import 'package:dev_tools/adapters/serial_stub.dart'
if (dart.library.io) 'package:dev_tools/adapters/desktop_serial.dart'
if (dart.library.html) 'package:dev_tools/adapters/web_serial.dart';
abstract class SerialInterface {
factory SerialInterface() => getSerialInterface();
dynamic getPort(String portName);
dynamic getSelectedPort();
List<String> getAvailablePorts();
bool close();
String get name;
bool get isOpen;
}
stub
import 'package:dev_tools/interfaces/Iserial.dart';
SerialInterface getSerialInterface() => throw UnsupportedError(
'Cannot create a keyfinder without the packages dart:html or package:shared_preferences');
desktop_serial
import 'package:dev_tools/interfaces/Iserial.dart';
SerialInterface getSerialInterface() => DesktopSerialInterface();
class DesktopSerialInterface implements SerialInterface {
// Map<String, SerialPort?> portList = {};
// SerialPort? _selectedPort;
// SerialPortReader? _serialPortReader;
// Stream? _incomingDataStream;
// String serialData = "";
DesktopSerialInterface() {
print("DesktopSerial Initialized");
}
@override
List<String> getAvailablePorts() {
List<String> serialNameList = [];
return serialNameList;
}
@override
dynamic getPort(String portName) {
return null;
}
@override
dynamic getSelectedPort() {
return null;
}
@override
bool close() {
return false;
}
String get name => throw UnimplementedError("name getter not implemented");
bool get isOpen => throw UnimplementedError("isOpen getter not implemented");
}
web_serial
import 'package:dev_tools/interfaces/Iserial.dart';
SerialInterface getSerialAdapter() => WebSerialInterface();
class WebSerialInterface implements SerialInterface {
// Map<String, SerialPort?> portList = {};
// SerialPort? _selectedPort;
WebSerialInterface() {
print("WebSerial Initialized");
// window.navigator.serial.requestPort().then((port) => _selectedPort = port);
// _selectedPort!.readable.reader.read().asStream()
}
@override
List<String> getAvailablePorts() {
List<String> serialNameList = [];
return serialNameList;
}
@override
dynamic getPort(String portName) {
// window.navigator.serial.requestPort().then((port) => _selectedPort = port);
return null;
}
@override
dynamic getSelectedPort() {
return null;
}
@override
bool close() {
return false;
}
String get name => throw UnimplementedError("name getter not implemented");
bool get isOpen => throw UnimplementedError("isOpen getter not implemented");
}
I am not sure what i am doing wrong. Since the desktop build is working and only the web build is not working i feel like the selective import is the one not working.
So is the dart.library.html is deprecated or something? since that is the point where we import the getSerialInterface funtion for web build
I stopped looking into this for a while since i couldn't figure this out. But when i got back to this in a fresh mind i saw what was wrong and i feel silly about that. but for people who are wondering what could be the issue.
SerialInterface getSerialAdapter() => WebSerialInterface();
needed to change into
SerialInterface getSerialInterface() => WebSerialInterface();