Make http call synchronous - flutter

I am using Flutter to develop an app. Based on the response from a REST API, I need to route to different screens. As HTTP calls in Flutter are asynchronous, I am struggling to keep it blocked until the response is received. Before the response is received, the routing logic is executed and routing occurs based on previously initialized values. How can I make call synchronously?
I tried using package:sync_http/sync_http.dart as below, but it always fails. There is not enough documentation with the package either.
SyncHttpClientRequest request = SyncHttpClient.postUrl(Uri.parse('http://{serveraddress}:{port}/{apiname}/$param1/$param2'));
SyncHttpClientResponse response = request.close();

Try the Flutter HTTP or DIO package.
You can make an asynchronous call using the await keyword.
For example:
var response = await http.post(url, body: your request body);
The above line of code will wait until it gets the response from HTTP.
Make sure to encode/decode your data.

Related

405 Method Not Allowed when using dio in Flutter

I am using flutter and flask as backend, when i am having a get request just after a post request am getting 405, Method Not allowed from the server. When i checked the server log I saw that the get request contains a body. Is it because dio is using a factory constructor to create object?
I tried clear method and still got the same issue!

Http status error [301] in response Dio Flutter

There is an api for authorization, I send a request using thunder client, everything seems to be ok, I get a token in response, but when I try to knock on the same link in the application itself using the dio library, I get an Http status error [301] in response, what could be the problem, I didn't really find an answer anywhere?
how do I send a request
final response = await _dio.post(
'http://someaddress.com/api/auth/login',
data: {"login": phone, "password": event.password},
);
Error:
Http 301 status code means that the link is not anymore available and new one is defined in Location header. The response looks like something like this.
HTTP/1.1 301 Moved Permanently
Location: https://foo.bar/foobar
Response headers in dio can be accessed by headers property.

Cache response body from http request in flutter

I am looking for a way to cache the response from a http get request and everytime the user opens the app i want to compare the data in the cache with the data from the request and display the http response instead of the cached data if there is any difference
You should try using the package flutter_cache_manager, that should fit your needs.

Unused web sockets requests in flutter network activity with almost same time as other http requests

I was going through my network activity in flutter dev tools. And I noticed that there were some web sockets requests being sent. I have programmed both the backend and the app. And I haven't used web sockets in anyway. All are plain http requests. And the weird thing is I own the IP address where the web sockets are being sent to. Here is the screenshot:
I am not sending or upgrading to web sockets. How to know who is sending this request? And why are there extra requests being sent?
Here is the call to authenticate for example:
Future<UserType> authenticate(User user) async {
final _log = Logger('authenticate');
_log.info('authenticating user');
final response = await http.post(
base_url + '/authenticate',
headers: {'Content-Type': 'application/json'},
body: jsonEncode({
'phone_number': user.phoneNumber.trim(),
'password': user.password.trim(),
// notification is a singleton somewhere with a string token
'fcm_token': notification.token,
}),
);
}
the POST http call is being upgraded to websocket each time you call the API, look into headers on both sides (default ones or the ones you are passing), 101 response is switching protocol response, as mentioned connection is being upgraded on request by the client
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/101
remove these headers Upgrade: WebSocket and Connection: Upgrade

How to create Http Response with Dart

I was trying to follow along with one of the Dart HttpServer examples from GitHub, and, while it does show how to create and handle routes, I'm still at a loss on how to produce and send an HttpResponce in response to a specific url being requested.
For example, while working with WebAPI, I would have an Api Controller, in which I could explicitly define an action for GET, PUT, DELETE and POST verbs, and return an appropriate HttpResponse with a resource from each such method.
Does anyone know how this typical CRUD business is done using Dart as an Http server?
Once you receive a HttpRequest you need to use the response attribute to answer.
void sendOk(HttpRequest request, [content]) {
request.response
..statusCode = HttpStatus.OK
..write(content)
..close()
;
}