Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dart 如何在类中指定函数参数_Dart - Fatal编程技术网

Dart 如何在类中指定函数参数

Dart 如何在类中指定函数参数,dart,Dart,我如何做这样的事情(getNames) 类APIServer{ 字符串名; 未来的getNames(字符串查询); APIServer({this.name,this.function}); } 您需要使用函数类型来声明函数变量 class APIServer { String name; Future<String> Function(String query) getNames; APIServer({this.name, this.getNames}); } 我

我如何做这样的事情(
getNames

类APIServer{
字符串名;
未来的getNames(字符串查询);
APIServer({this.name,this.function});
}

您需要使用
函数
类型来声明函数变量

class APIServer {
  String name;
  Future<String> Function(String query) getNames;

  APIServer({this.name, this.getNames});
}

我不明白。你能描述一下你希望结果是什么吗?VSCode说它不是一个抽象类,所以我不能这样做(?)-
“getNames”必须有一个方法体,因为“APIServer”不是抽象的。尝试将“APIServer”抽象化,或者将主体添加到“getNames”
中,您现在设置的
getNames
是一种抽象方法,您会遇到这种错误,因为
APIServer
不是抽象类。你想做什么?我想说“嘿,你想创建一个新的APIServer吗?好的,给我一个名称和一个函数,它返回一个未来并接收一个‘字符串查询’参数”
typedef NameGetter = Future<String> Function(String query);

class APIServer {
  String name;
  NameGetter getNames;

  APIServer({this.name, this.getNames});
}