5

I am new in flutter. I need to call 5 API network calls in a single screen. It is taking very long time while i am using Async/await. How can we execute it on separate threads parallelly using isolate or anything else like it?

James Z
  • 12,209
  • 10
  • 24
  • 44
abdulec90
  • 262
  • 3
  • 11
  • 1
    You can use .then instead of await so and get value to replace what you need when it’s done without blocking each api – Einzeln Jul 03 '21 at 14:26

3 Answers3

3

You may use isolate for this purpose isolate is a sort of multi threading in dart. Isolate creates a new thread and execute operation on the new thread so that the load will be distributed. You cannot send variables as a data back and forth but use port to send messages.

Here is a simple example of isolate with an API call and sending data back to the main thread using port.

First lets create a function which will be the entrypoint of isolate:

static entryPoint(SendPort sendPort)async{
    var response = await http.get('https://www.thecocktaildb.com/api/json/v1/1/search.php?s=margarita');
    sendPort.send(response.body); //sending data back to main thread's function
}

Now lets create isolate:

static void callApi()async{
    var recievePort = new ReceivePort(); //creating new port to listen data
    await Isolate.spawn(entryPoint, recievePort.sendPort);//spawing/creating new thread as isolates.
    recievePort.listen((message) {  //listening data from isolate
      print(message);
    });
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Abhay Kumar
  • 293
  • 2
  • 5
  • How can I return the result to main isolate?? any code example please. – abdulec90 Jul 14 '21 at 04:18
  • 1
    You cant return or share the data. one thing you can do is that send message using the send port and receive the message the message in the port as i have done in the answer. If you want to know further open a new question and provide the link here. – Abhay Kumar Jul 18 '21 at 04:09
  • @AbhayKumar hi, nice explaination and an example, Can i add Isolate inside Future.wait? For example, I have 300 ids, and I try to implemenet like this Future.wait(ids.map((id) => Isolate.spawn(reqData, id)); and the process is not running, did it right to implement Isolate that way? Thank you. – MNFS Jun 23 '22 at 08:02
1

You can use dio package and call multiple concurrent API requests, do check the documentation:

Package: https://pub.dev/packages/dio
RTXGamer
  • 3,215
  • 6
  • 20
  • 29
  • I tried Dio package , but it seems it is working as normal http package. can you help with some sample code for concurrent API request – abdulec90 Jul 13 '21 at 14:50
0

You can use Future.wait()

Here is the answer with an example

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Mertus
  • 1,145
  • 11
  • 17
  • I tried Future.wait(). but it seems it execute all the calls on the same thread. so it also taking long time, although it is executing parallelly. – abdulec90 Jul 03 '21 at 14:06
  • I thought the issue was the time it takes to receive a response from each call. If you have cpu bound computations to perform after receiving a response then isolates are the way to go. – Mertus Jul 04 '21 at 11:13