AVEbrahimi
AVEbrahimi

Reputation: 19154

Flutter compute function not called if parameter is not a primitive?

I tried to send a Map to a compute, but computer is never called. The strange point is if I replace Map with int, it works:

void A()
{
    var map=Map();
    map["p1"]=90;
    D("before compute");
    var r1 = await compute(p1, 10);
    D("after compute(p1) : $r1");
    var r2 = await compute(p2, map);
    // code never reaches here!
    D("after compute(p2) : $r2");
}

static int p2(Map p)
  {
    return p["p1"]*10;
  }

static int p1(int z)
  {
    return z*10;
  }

output is : after compute(p1) : 100

Upvotes: 0

Views: 2590

Answers (2)

Just define exact type of Map that "p2" receives as parameter:

static int p2(Map<String,int> p)
 {
    return p["p1"]*10;
 }

Try passing a const parameter:

var r2 = await compute(p2, {"p1":90});

Upvotes: 0

Erkan
Erkan

Reputation: 766

Flutter compute methods use Isolates and its only transfer (null, num, bool, double, String) types.

https://api.flutter.dev/flutter/dart-isolate/SendPort/send.html

Upvotes: 2

Related Questions