How to create an isolate for recursive function in flutter?

278 Views Asked by At

How can I use isolate to get Fibonacci series in flutter? Actually, I have some bigger problems in which I have to do recursion in large list. By your answer to this question, I will get the tip to my original problem.

int Fib(int n){
  if(n<=1) //Base Condition
    return n;
  return Fib(n-1)+Fib(n-2);
}
  
  
void main() {
  print(Fib(6));
}

I don't know where I am missing, and I am getting the following error: Invalid argument(s): Illegal argument in isolate message: (object extends NativeWrapper - Library:'dart:ui' Class: Paragraph

1

There are 1 best solutions below

0
On

Declare the function in an empty dart file not in a class or outside the scope of a class (It should be a top-level function)

int Fib(int n){
  if(n<=1) //Base Condition
    return n;
  return Fib(n-1)+Fib(n-2);
}

Then access it anywhere like this:

int result = await compute(Fib,6);