Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

dart - Flutter- compute method

I'm trying to execute heavy method by compute().
I tried like so. After loop executed, Text widget update but result returns 0 almost right after button was pressed.
Does anyone know what am I missing?

 int _counter;

  static int loop(int val) {
    int count = 0;
    for (int i = 1; i <= val; i++) {
      count += i;
    }
    return count;
  }

  Future<void> _onPressed() async {
    int result = await compute(loop, 1000000000000000000);
    setState(() {
      _counter = result;
    });
  }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

According to https://api.flutter.dev/flutter/foundation/compute-constant.html,

The callback argument must be a top-level function, not a closure or an instance or static method of a class.

Please remove the static keyword, it should work fine


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...