向 Flutter 中的计算函数发送多个参数

Nat*_*hat 12 dart flutter

我试图在 Flutter 中使用计算功能。

void _blockPressHandler(int row, int col) async {
//    Called when user clicks any block on the sudoku board . row and col are the corresponding row and col values ;
    setState(() {
      widget.selCol = col;
      }
    });

    bool boardSolvable;
    boardSolvable = await compute(SudokuAlgorithm.isBoardInSudoku , widget.board , widget.size) ;

  }
Run Code Online (Sandbox Code Playgroud)

isBoardInSudoku是SudokuAlgorithm 类的静态方法。它存在于另一个文件中。编写上面的代码,告诉我

error: The argument type '(List<List<int>>, int) ? bool' can't be assigned to the parameter type '(List<List<int>>) ? bool'. (argument_type_not_assignable at [just_sudoku] lib/sudoku/SudokuPage.dart:161)

我该如何解决?可以在不将 SudokuAlgorithm 类的方法从其文件中取出的情况下完成吗?如何向计算函数发送多个参数?

static bool isBoardInSudoku(List<List<int>>board , int size ){ } 是我的 isBoardInSudoku 函数。

Gün*_*uer 14

只需将参数放在 Map 中并传递它。

没有办法传递一个以上的参数,compute因为它是一个方便的函数来启动隔离,除了一个参数之外,它也不允许任何东西。


Kir*_*zin 8

在 OOP 中以及一般情况下,为您需要的字段创建一个classfor 更为优雅,这为您提供了更大的灵活性,并减少了硬编码字符串或键名称常量的麻烦。

例如:

boardSolvable = await compute(SudokuAlgorithm.isBoardInSudoku , widget.board , widget.size) ;

用。。。来代替

class BoardSize{
  final int board;
  final int size;
  BoardSize(this.board, this.size);
}

...

boardSolvable = await compute(SudokuAlgorithm.isBoardInSudoku, BoardSize(widget.board, widget.size)) ;
Run Code Online (Sandbox Code Playgroud)


liv*_*ove 5

使用地图。下面是一个例子:

Map map = Map();
map['val1'] = val1;
map['val2'] = val2;
Future future1 = compute(longOp, map);


Future<double> longOp(map) async {
  var val1 = map['val1'];
  var val2 = map['val2'];
   ...
}
Run Code Online (Sandbox Code Playgroud)