f3ath / json-api-dart

JSON:API client and server for Dart/Flutter
https://pub.dartlang.org/packages/json_api
MIT License
79 stars 24 forks source link

Throw error when decode the body: Bad state: Stream has already been listened to. #140

Closed solody closed 1 month ago

solody commented 3 months ago

Throw error when decode the body:

final response = await client.jsonapiClient.fetchCollection('node--gallery', query: [Query({'include': ['field_gallery_image']})]);
final json = await response.httpResponse.body.decode(utf8);

Bad state: Stream has already been listened to.

solody commented 3 months ago

I can find a way to get the response body as a dart String, when I try to use the japx package to flattens the response.

f3ath commented 3 months ago

I think it is impossible to read the http response stream twice. However the raw parsed json is available as response.document. Can you give more details about your use case? Why do you need to decode the raw response again?

solody commented 1 month ago

package:json_api is amazing, I love it, but the package:japx provides a more efficient way to read the response of json api. So i want to get the raw json to construct a japx object, unfortunate package:json_api doesn't return the raw response body:

https://github.com/f3ath/json-api-dart/blob/master/lib/src/client/response/collection_fetched.dart

solody commented 1 month ago

response.document

I think it is impossible to read the http response stream twice. However the raw parsed json is available as response.document. Can you give more details about your use case? Why do you need to decode the raw response again?

Unfortunate the response.document also can not be accessed from collection_fetched.dart

f3ath commented 1 month ago

Ah I see what you mean. I will tweak the RoutingClient responses and expose the raw document, should be straightforward enough. Meanwhile you can try using the low-level Client class as a workaround.

solody commented 1 month ago

Ah I see what you mean. I will tweak the RoutingClient responses and expose the raw document, should be straightforward enough. Meanwhile you can try using the low-level Client class as a workaround.

Thank you for you help, but if I use the low-level Client class, why do I use json-api-dart package, haha

I've fork this repository, and try to contribute a branch for this issuse, I'll come back soon.

solody commented 1 month ago

How about include the https://pub.dev/packages/japx package to provide more efficient way to read jsonapi response in our json_api package?

f3ath commented 1 month ago

How about include the https://pub.dev/packages/japx package to provide more efficient way to read jsonapi response in our json_api package?

I don't see significant benefits in including it as a dependency. Rather this package should be flexible enough to allow combining it with other tools if needed.

I've exposed the rawResponse object, let me know if that helps: https://pub.dev/documentation/json_api/latest/client/CollectionFetched/rawResponse.html