skip to Main Content

in my function there is a warning about the return (This function has a return type of ‘int’, but doesn’t end with a return statement. ) but I dont know what is the problem . any idea?

int checkValueIndex() {
   if (selectedValue != null) {
      for (int i = 0; i < mList.length; i++) {
        if (mList[i].name == selectedValue.name) {
          return i;
            }
          }
        } else {
          return null;
      }
     }

2

Answers


  1. Error says you have to return some int value. Add ? in return type so that you can allow return null value as well.

    try this code:

    int? checkValueIndex() {
      if (selectedValue != null) {
        for (int i = 0; i < mList.length; i++) {
          if (mList[i].name == selectedValue.name) {
            return i;
          }
        }
      } else {
        return null;
      }
    }
    
    Login or Signup to reply.
  2. Your function return null value ,add (?) so it will accept null as follow:

     int? checkValueIndex() {
       if (selectedValue != null) {
          for (int i = 0; i < mList.length; i++) {
            if (mList[i].name == selectedValue.name) {
              return i;
                }
              }
            } else {
              return null;
          }
         }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search