What is the best way to make multiple requests to the same server in Dart?
dart, http
Solution
First, use the `http` Pub package.
For making multiple requests to the same server, keep a persistent connection open by using `http.Client`. This is better than making multiple single requests. Here is some code that shows how this can be done:
import 'package:http/http.dart' as http;
void main() {
var url = 'http://httpbin.org';
var client = new http.Client();
client.get('${url}/foo')
.then((response) {
print(response.body);
return client.get('${url}/bar');
})
.then((response) {
print(response.body);
});
.whenComplete(client.close);
}
Be sure to close the client connection when done.
Problem
I want to make server HTTP requests to the same server and was wondering if there was an efficient way to do so by keeping a persistent connection open?