FengChendian / serial_port_win32

A flutter SerialPort library using win32 API.
BSD 3-Clause "New" or "Revised" License
31 stars 9 forks source link

Getting this error on hot restarting #19

Open Kapish26 opened 2 years ago

Kapish26 commented 2 years ago

Getting this error on hot restarting!!! Please help

The following _Exception was thrown while handling a gesture:
Exception: Last error is 0

When the exception was thrown, this was the stack: 
#0      SerialPort.open (package:serial_port_win32/src/serial_port.dart:169:11)
#1      _MyHomePageState._getPortsAndOpen (package:demo/main.dart:43:14)
#2      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:1005:21)
#3      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:198:24)
#4      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:613:11)
#5      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:298:5)
#6      BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:269:7)
#7      GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:157:27)
#8      GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:449:20)
#9      GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:425:22)
#10     RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:329:11)
#11     GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:380:7)
#12     GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:344:5)
#13     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:302:7)
#14     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:285:7)
#18     _invoke1 (dart:ui/hooks.dart:170:10)
#19     PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:331:7)
#20     _dispatchPointerDataPacket (dart:ui/hooks.dart:94:31)
(elided 3 frames from dart:async)
Handler: "onTap"
Recognizer: TapGestureRecognizer#774c2
  debugOwner: GestureDetector
  state: ready
  won arena
  finalPosition: Offset(1212.8, 640.8)
  finalLocalPosition: Offset(19.2, 30.4)
  button: 1
  sent tap down
====================================================================================================
FengChendian commented 2 years ago

Emm... I think it's due to open function in tap property. Hot reload may reload open function again. But you have opened this port or you don't have any available port. So open is failed.

Maybe you can use if condition to judge open status and port list.

And about last error is 0, it's a bug due to VM. Last error is always 0. So we don't know real error code. Refer to https://github.com/timsneath/win32/issues/189

Here is code of line 169 in serial port. It's open exception and it's always fallback to last statement (if VM bug not fixed)

        if (lastError == ERROR_FILE_NOT_FOUND) { 
           throw Exception(_portNameUtf16.toDartString() + "is not available"); 
         } else { 
           throw Exception('Last error is $lastError'); 
         }

By the way, I think the simple way is to use try catch block to ignore this exception. Because last error is always zero...