Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Im getting this error I cant resolve in flutter

Future<String> task2() async {
  Duration three = const Duration(seconds: 3);
  String result;

  await Future.delayed(three, () {
    result = 'task 2 data';
  });
  return result;
}

The non nullable local variable ‘result’ must be assigned before it can be used.

now if i assign it a value on initiation or add a null check, the function just returns that value and not the intended value task 2 data

Can someone identify the problem and help me resolve it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need call then like this:

await Future.delayed(Duration(seconds: 2)).then((_) {
      result = 'task 2 data';
});

or whenComplete:

await Future.delayed(Duration(seconds: 2)).whenComplete(() => result = 'task 2 data');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading