static Location fetchById(int id) {
   List<Location> locations = Location.fetchAll();

   for (var i = 0; i < locations.length; i++) {
     if (locations[i].id == id) {
       return locations[i];
     }
   }

   return null;
}

// if the condition is not true then return null when I try to return null or false it gives the error 'A value of type 'Null' can't be returned from the method 'fetchById' because it has a return type of 'Location'.


Solution 1: nicks101

With null-safety feature in the dart language, you have to explicitly tell if you want to make a value nullable.

Define the return type with a ?, so dart knows that return value can be null.

static Location? fetchById(int id) 
{ 
  /// function body
}