Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[页面问题] '深入理解 Function #1439

Open
1 task
DivvSpace opened this issue Apr 26, 2024 · 0 comments
Open
1 task

[页面问题] '深入理解 Function #1439

DivvSpace opened this issue Apr 26, 2024 · 0 comments
Labels
E: 页面问题 页面内容有误或需要改进。

Comments

@DivvSpace
Copy link

页面 URL

https://flutter.cn/community/tutorials/deep-dive-into-dart-s-function-closure/

页面源代码地址

https://github.com/cfug/flutter.cn/tree/main/src/community/tutorials/deep-dive-into-dart-s-function-closure.md

描述问题

实现策略模式中,代码错误

void main(){
  var res = exec(select('sum'),1 ,2);
  print(res);
}

Function select(String opType){
  if(opType == 'sum') return sum;
  if(opType == 'sub') return sub;
  return (a, b) => 0;
}

int exec(NumberOp op, int a, int b){
  return op(a,b);
}

int sum(int a, int b) => a + b;
int sub(int a, int b) => a - b;

typedef NumberOp = Function (int a, int b);

Error: The argument type 'Function' can't be assigned to the parameter type 'dynamic Function(int, int)'.

期望如何修复

Function select(String opType){
// ...
}

// 返回类型改为:NumberOp即可
NumberOp select(String opType){
// ...
}

附加信息

No response

我希望能自己修复这个问题

  • 我将自己尝试修复这个问题
@DivvSpace DivvSpace added the E: 页面问题 页面内容有误或需要改进。 label Apr 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E: 页面问题 页面内容有误或需要改进。
Projects
None yet
Development

No branches or pull requests

1 participant