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

Why can't a general function performs a loop on any given list work

I wrote this function to perform a loop on any list

int loop(List list) {
  for (var i = 0; i < list.length; i++) {
    int currentValue = i;
    list[currentValue];
  }
}

but it says

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

I added a throw statement but after a single loop the second time, it throws the exception. And I have no idea what to return outside the for-loop.

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 :

The body might complete normally, causing ‘null’ to be returned

Your method return type is int, means you need to return a int from the method.

int loop(List list) {
  for (var i = 0; i < list.length; i++) {
    int currentValue = i;
    list[currentValue];
  }
  return 0; //any int
}
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