Flutter:类型'Future<bool?>'不是类型'FutureOr<bool>'在类型转换中的子类型

0 人关注

I'm new on the Flutter & working on the integration of POS printing machine in flutter & using the pos_printer_manager 包。

它显示在这个包的捕获部分有一个错误,即 【替换代码0

& pointing out in this code

/// [writeBytes] let you write raw list int data into socket
  @override
  Future<ConnectionResponse> writeBytes(List<int> data,
      {bool isDisconnect: true}) async {
    try {
      if (!isConnected) {
        await connect();
      if (Platform.isAndroid || Platform.isIOS) {
        if ((await (bluetooth.isConnected as FutureOr<bool>))) {
          Uint8List message = Uint8List.fromList(data);
          PosPrinterManager.logger.warning("message.length ${message.length}");
          await bluetooth.writeBytes(message);
          if (isDisconnect) {
            await disconnect();
          return ConnectionResponse.success;
        return ConnectionResponse.printerNotConnected;
      //  else if (Platform.isIOS) {
      //   // var services = (await fbdevice.discoverServices());
      //   // var service = services.firstWhere((e) => e.isPrimary);
      //   // var charactor =
      //   //     service.characteristics.firstWhere((e) => e.properties.write);
      //   // await charactor?.write(data, withoutResponse: true);
      //   return ConnectionResponse.success;
      return ConnectionResponse.unsupport;
    } catch (e) {
      print("Error : $e");
      return ConnectionResponse.unknown;

This is due to bluetooth.isConnected as FutureOr<bool>.

So any big difference between Future<bool?> & FutureOr<bool> ?

Basically I faced type casting error in the package & I need a solution to handle this on the package side & how to manage the optional.

android
ios
flutter
dart
thermal-printer
Ankush Lokhande
Ankush Lokhande
发布于 2022-12-29
2 个回答
Abhi Tripathi
Abhi Tripathi
发布于 2022-12-29
0 人赞同

根据你的发现,typecast是不需要的,它需要一个空检查

if (Platform.isAndroid || Platform.isIOS) {
         bool? isConnected  = await bluetooth.isConnected;
        if (isConnected != null && isConnected!) {
          Uint8List message = Uint8List.fromList(data);
          PosPrinterManager.logger.warning("message.length ${message.length}");
          await bluetooth.writeBytes(message);
          if (isDisconnect) {
            await disconnect();
          return ConnectionResponse.success;
        return ConnectionResponse.printerNotConnected;