Flutter Parallel Network call
Asked Answered
Q

3

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?

Quinonoid answered 3/7, 2021 at 12:21 Comment(1)
You can use .then instead of await so and get value to replace what you need when it’s done without blocking each apiPastoralist
L
7

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);
    });
}
Louisalouisburg answered 4/7, 2021 at 2:8 Comment(3)
How can I return the result to main isolate?? any code example please.Quinonoid
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.Louisalouisburg
@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.Councilor
B
1

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

Package: https://pub.dev/packages/dio
Buchanan answered 3/7, 2021 at 12:37 Comment(1)
I tried Dio package , but it seems it is working as normal http package. can you help with some sample code for concurrent API requestQuinonoid
C
0

You can use Future.wait()

Here is the answer with an example

Cylix answered 3/7, 2021 at 13:51 Comment(2)
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.Quinonoid
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.Cylix

© 2022 - 2024 — McMap. All rights reserved.