使用listTimes方法时无法运行
void main() { // 方法赋值给变量 var fun = printHello; fun(); // 方法作为参数传递 List list = [1, 2, 3, 4]; list.forEach(print); List names = ["哈", "王", "神"]; print(listTimes(names, times)); } /// 方法 void printHello() { print("Hello"); } /// 创建了一个传入函数的方法 List listTimes(List list, String times(msg)) { for (int i = 0; i < list.length; i++) { list[i] = times(list[i]); } return list; } String times(String msg) { return msg * 3; }
在调用
print(listTimes(names, times));
时软件报错,提示:
Error: The top level function has type 'String Function(String)' that isn't of expected type 'String Function(dynamic)'.
Change the type of the function or the context in which it is used.
print(listTimes(names, times));