Get an error for some specific request Bad state: The 'read' method can only be called once on a shelf.Request/shelf.Response object.
SittiphanSittisak opened this issue ยท 1 comments
Asynchronous error 0|server_dart_project | Bad state: The 'read' method can only be called once on a shelf.Request/shelf.Response object. 0|server_dart_project | package:shelf/shelf_io.dart 167:11 handleRequest
My repo.
For the server_dart, I got this error when my flutter app tried to connect this server. Sometimes it works fine, sometimes it always gets the error.
Yesterday I can connect API successfully with browser A but B did not.
Today browser A can't connect this API with success too.
If I restart the server and connect this API with browser B before A, B can connect with success but A can't. Sometimes after restarting the server, A and B can connect but C not.
I tried with
final _router = shelf_router.Router()
..post(ApiPath.sendOtp, (Request request) => Response(HttpStatus.badRequest, body: jsonEncode({'message': 'A'})))
..post(ApiPath.getApplicationForm, (Request request) => Response(HttpStatus.badRequest, body: jsonEncode({'message': 'B'})))
..post(ApiPath.saveApplicationForm, (Request request) => Response(HttpStatus.badRequest, body: jsonEncode({'message': 'C'})))
..all('/<ignored|.*>', (Request request) {
if (request.method == 'OPTIONS') return ServerSetting.preflightResponse;
return Response.internalServerError();
});
And it still happens. It looks like browser A connects API 1 for the first time.
A will send preflight with the OPTIONS method to API 1. After it succeeds, A will send a POST request without preflight to API 1 but if it didn't succeed(this error happens), it will always error. And I don't know why this error happened. But if an API 1 was an error, all devices that tried to connect this API 1 the first time(send preflight request) will always error.
What happens? How to fix that?
This doesn't seem like a shelf issue.
You CAN'T use a final/const response instance...it looks like you may have that in ServerSettings.preflightResponse
maybe turn that into a getter that returns a new instance for every request?