1

I am creating an Application for Kiosk Devices which is running the API recursively and updating the data on Screen, but I realized after some days the App got Crashed due to a recursive function it's a StackOverflow Error. What is the solution to avoid this error or the best approach to Call Infinite Recurising API with delay?

Following is the Approach, I am using in my app

CallRestApi(){
   await getResponseFromServer();
   updateUi();
   await Future.delayed(Duration(seconds: 2));
   CallRestApi();
}

Edit: I also used the timer approach but result is same

MRazaImtiaz
  • 1,964
  • 1
  • 13
  • 23

1 Answers1

1

For starters, you could make your call non-recursive by using a simple loop instead of recursion:

Future<void> CallRestApi() async{
   while(true) {
     await getResponseFromServer();
     updateUi();
     await Future.delayed(Duration(seconds: 2));
   }
}

That still is suspicious and probably not the best way to do this.

Now I don't know your use case, but you may want to look into a few options to get rid of this for good:

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • I will try to use this solution, my use case is to Show the Ticket Number On the Tv Screen and the doctor's image. The ticket Number will be changed whenever the doctor will call the patient and I have to check every second for a ticket number – MRazaImtiaz May 09 '22 at 06:55
  • 1
    You could also try a different kind of network connection. Something that could be updated every second is not a good usecase for manual polling via http. If your backend is .NET you could use SignalR, I think Google's GRPC can do something similar. – nvoigt May 09 '22 at 06:58