This error can occur after upgrading to drupal 9. 

Call to undefined method Drupal\file\Entity\File::url()

Solution

Change

$url = $file->url();

to

$url = $file->createFileUrl();