Flutter - initState期望Future的返回值< String>

uxhixvfz  于 2023-05-29  发布在  Flutter
关注(0)|答案(1)|浏览(178)

我有代码工作之前,但在引入空安全,这下面的代码是打破与错误

The body might complete normally, causing 'null' to be returned, but the return type, 'Future<String?>', is a potentially non-nullable type.
Try adding either a return or a throw statement at the end.

下面是代码,我不确定,什么需要改变

@override
  void initState() {
    SystemChannels.lifecycle.setMessageHandler((msg) {
      // debugPrint('SystemChannels> $msg');
      super.initState();

      if (msg == AppLifecycleState.resumed.toString()) {
        // print("in resumed of payments");
        setState(() {
          dataLoaded = false;
        });
        customerPayments();
      } else {
        throw Exception("In Else");
      }
    });
  }

customerPayments()是void方法。

c0vxltue

c0vxltue1#

您在initState中声明了一个函数,该函数需要为每个可能的结果返回一个值。然而,除了其他问题,你不应该在你声明的函数中调用super.initState()。你可以在之前或之后调用它,这取决于你在做什么。
从本质上讲,这个特定的错误消息基本上是说,就Flutter所知,函数有可能在没有潜在返回值的情况下结束,从而返回null。
https://api.flutter.dev/flutter/widgets/State/initState.html

@override
void initState() {
  SystemChannels.lifecycle.setMessageHandler((msg) {
    // debugPrint('SystemChannels> $msg');
    super.initState();

    if (msg == AppLifecycleState.resumed.toString()) {
      // print("in resumed of payments");
      setState(() {
        dataLoaded = false;
      });
      customerPayments();
    } else {
      throw Exception("In Else");
    }
    //Return statement would go here.
  });
}

相关问题