| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 |
- 일상
- 개발
- 에러
- 템플릿
- 연습문제
- 휘발류
- 인프라
- 퇴근길
- podman
- 개발기
- 가상머신
- 객체지향프로그래밍
- 오블완
- virtualmachine
- Bloc
- 티스토리챌린지
- node.js
- C언어
- K&R
- 잡담
- Linux
- ubuntu
- VMware
- DART
- VM
- Flutter
- 주유소
- FreeBSD
- 리눅스
- 오토바이
- Today
- Total
그냥저냥
Dart/Flutter | flutter_secure_storage 사용 중 Error: OperationError 에러 해결하기 본문
Dart/Flutter | flutter_secure_storage 사용 중 Error: OperationError 에러 해결하기
sync86 2024. 12. 20. 18:07Flutter로 모바일 앱 개발을 하면서 flutter_secure_storage를 로그인했을 때 서버에서 받은 토큰을 저장하는 용도로 사용하고 있다. 그런데 웹으로 실행한 후 로그인을 시도하고 새로고침을 하면 Error: OperationError가 발생하는 것이다. 안드로이드, iOS, macOS에서 실행할 때는 특이사항이 없었던 것 같다.

맨 처음 이 에러를 접했을 때 당연하게도 "웹 지원X"인 줄 알았다. 그래서 pub.dev에 flutter_secure_storage에서 README 항목을 유심히 살펴보았다.
flutter_secure_storage | Flutter package
Flutter Secure Storage provides API to store data in secure storage. Keychain is used in iOS, KeyStore based solution is used in Android.
pub.dev
어머나! 웹도 지원된다?

그러면 좀 더 내려보면 웹 버전에 대한 설명이 있을 것 같아 스크롤을 좀 더 내려보았다. 예상대로 있었다. flutter_secure_storage는 브라우저에 내장된 WebCrypto를 사용한다고 시험적으로 사용할 수 있다는 내용과 함께 "HTTP Strict Transport Security"를 헤더를 활성화 해야 한다고 한다.

그래서 HTTP Strict Transport Security를 서버 헤더에서 추가하였다. 지난번에는 어찌어찌 같은데 이번에는 같은 에러가 계속 발생하였다.
https://github.com/juliansteenbakker/flutter_secure_storage/issues/381
OperationError when calling `read()` on web · Issue #381 · juliansteenbakker/flutter_secure_storage
I'm getting the following error when calling read() on web, only if the key exists in storage. It works fine if the key does not exist. Calling containsKey() returns the correct value and does not ...
github.com
원인은 "apetroaeiandrei" 댓글에서 힌트를 얻을 수 있었다. 좀 이해가 안되지만 스토리지에 동시에 write를 해서 문제가 생겼다는 것이다. 그러나 작성된 코드는 동시에 write 하지 않았다. 댓글 중 "thinkingatoms"은 아래와 같이 synchronized 패키지를 설치해서 해결했다는 이야기를 볼 수 있었다.
await _lock.synchronized(() async {
// ... 코드 ...
});
https://pub.dev/packages/synchronized
synchronized | Dart package
Lock mechanism to prevent concurrent access to asynchronous code.
pub.dev
다행히도 synchronized 패키지를 사용하여 코드를 변경한 후 Error: OperationError 에러는 보이지 않는다. 해결된 것 같다.