문제 설명
Aqueduct 및 Dart: Future<List>(T = ManagedObject)를 JSON으로 직렬화할 수 없습니다. (Aqueduct and Dart: Future<List> (T = ManagedObject) can't be serialized to JSON)
Aqueduct와 Dart에 익숙해지기 위해 여기 자습서를 따르고 있었습니다. 파트 1의 코드는 제대로 작동했지만 메모리 내 영웅 목록에서 데이터베이스 사용으로 변환하면 작동이 중지되었습니다. 데이터베이스(Postgres)에 데이터가 있고 백엔드가 성공적으로 연결되어 DB에서 읽는 것을 확인했습니다. 그러나 ManagedObject를 직렬화할 수 없다는 오류가 발생했습니다. 이 튜토리얼에서는 직렬화에 대한 세부 정보를 언급하지 않습니다. 내 모델 클래스에 @JsonSerializable을 추가하려고 시도했지만 여전히 동일한 문제가 있었습니다. 그것은 모델 개체 자체가 아니라 API가 다음을 반환한다는 사실인 것 같습니다. Future> </코드>? 오류:
Converting object to an encodable object failed: Instance of 'Future<List<Hero>>' #0 _JsonStringifier.writeObject (dart:convert/json.dart:649:7)
튜토리얼에서는 ManagedObject를 확장하는 클래스가 기본적으로/자동으로 직렬화 가능하다고 제안하는 것 같습니다. 그 가정이 잘못된 것인가? Aqueduct 웹사이트에서 바로 사용할 수 있는 튜토리얼이 있다는 것이 이상하게 보입니다.
참조 솔루션
방법 1:
I was missing the await keyword on the database query which explains why the API call was returning a Future> instead of the actual List. Doh!
(by Michael Batchelder、Michael Batchelder)
참조 문서