0

currently, I used http, I found a way to send multiple requests once to react using axios.

axios.all([
    axios.get('http://google.com'),
    axios.get('http://apple.com')
  ])
  .then(axios.spread((googleRes, appleRes) => {
    // do something with both responses
  });

Like this Is that any way to send multiple requests once?

1 Answers1

0

@mezoni answer is correct. But this is less code with caching also.

import 'dart:async';
import 'dart:convert';

import 'package:http/http.dart' as http;

void main() async {
  final urlList = ['http://google.com', 'http://apple.com'];

  final responses = await Future.wait(
    urlList.map((String url) {
      return http.get(url);
    }),
  );

  final List<dynamic> caches = responses.map((response) {
    return json.decode(response.body);
  }).toList();
}

Mehmet Esen
  • 6,156
  • 3
  • 25
  • 44