Home > OS >  The body might complete normally, causing 'null' to be returned, but the return type, 
The body might complete normally, causing 'null' to be returned, but the return type, 

Time:01-17

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

when I try to create string method:

String _environment() {
    PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
      switch (packageInfo.packageName) {
        case 'hbhbh':
          return '';
        default:
          return '';
      }
    });
  }

where the error code?

CodePudding user response:

It is because the String value is returned only in the then part of the promise

You are using async request , so it is preffered to wait for the request using await and add a default return just to avoid the error.

String _environment() async {
    await PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
      switch (packageInfo.packageName) {
        case 'hbhbh':
          return '';
        default:
          return '';
      }
    });
    return '';            
  • Related