这个错误通常是因为您在使用一个函数时,该函数返回了 null,但是该函数的上下文要求返回 void。例如:

void main() {
  Function myFunction = () {
    print('Hello World');
    return null; // 这里返回了 null
  };
  myFunction(); // 报错:The return type 'Null Function()' isn't a 'void', as required by the closure's context.
}

要解决这个问题,您可以将函数返回类型更改为 void,或者确保函数不会返回 null。例如:

void main() {
  void myFunction() { // 将返回类型更改为 void
    print('Hello World');
  };
  myFunction(); // 没有报错
}

或者:

void main() {
  Function myFunction = () {
    print('Hello World');
    return ''; // 确保函数不会返回 null
  };
  myFunction(); // 没有报错
}
``
flutter The return type Null Function isnt a void as required by the closures context 这个报错是怎么回事?

原文地址: https://www.cveoy.top/t/topic/cEnn 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录