Flutter

'RawKeyboard' is deprecated and shouldn't be used.

MORAN9 2024. 4. 8. 12:30
반응형
'RawKeyboard' is deprecated and shouldn't be used. Use HardwareKeyboard instead. This feature was deprecated after v3.18.0-2.0.pre.
Try replacing the use of the deprecated member with the replacement.dartdeprecated_member_use)

에러라기보다는 이러한 경고문이 나타나네요. 구글링에서 보편적으로 추천되는 클래스인 RawKeyboard가 deprecated(* 추후 사용되지 않을 함수나 기능, 클래스 등으로 사용하지 않는 것이 좋습니다.)된 모양입니다.

 

위 경고문과 같은 내용을 VSC에서 단순 캡처해둔 내용.

제 경우에는 Ctrl Press 여부에 따라 마우스 모양을 변경해주는 기능을 구현하려던 도중 위와 같은 경고문이 나타났고요.

 

 

[ 해결법 ]

이와 같은 RawKeyboard 인스턴스 대신에

// Control Left 키가 눌러졌는지를 bool Type으로 리턴해주는 구문
RawKeyboard.instance.keysPressed.contains(LogicalKeyboardKey.controlLeft)

 

아래와 같은 HardwareKeyboard 인스턴스로 바꿔주시면 경고문 없이 잘 작동합니다.

// Control 키가 눌러졌는지를 bool Type으로 리턴해주는 구문
HardwareKeyboard.instance.isControlPressed

개인적으로 RawKeyboard에서 사용하는 것처럼 LogicalKeyboardKey.controlLeft와 같은 경우, 키보드 타입에 따라 인식이 안되거나, 오른쪽 왼쪽 컨트롤 키를 따로 설정해주어야 하는 경우 등이 염려되었는데, 새로운 버전 깔끔하고 좋네요. 실제로 어떻게 작동할지는 아직 테스트를 안해봐서 모르겠지만, 코드만 보기에는 구 버전보다 좋아보입니다.

 


 

+ 추가: 아래와 같은 에러가 나타난다 하시는 분들:

Error: Assertion failed: file:///.../flutter/lib/src/services/hardware_keyboard.dart:1067:16
false
"Should never encounter KeyData when transitMode is rawKeyData."

 

Flutter 프로젝트 실행하실 때, canvaskit 대신 html로 실행하시면 됩니다.

// 개발 단계에서는 아래 명령어를 사용하여 실행
flutter run -d chrome --web-renderer html

// Build의 경우, 아래 명령어를 사용
flutter build web --web-renderer html

 

추가 에러 해결법 출처:

 

Flutter Web : "Should never encounter KeyData when transitMode is rawKeyData."

When I run my project on web the Exception message and stack trace was "Should never encounter KeyData when transitMode is rawKeyData." at Object.throw_ [as throw] (http://localhost:2...

stackoverflow.com

 

반응형