How to create an 'Upload a file' field? I have a content entity type, and I would like to have an 'Upload file' field. I have declared the field in my Entity class

$fields['file'] = BaseFieldDefinition::create('file')
    -&gt;setLabel(t('Upload a file'))
    -&gt;setDescription(t('Choose file from your computer'));


And in EntityForm I am trying to set some settings, because I need to make this dependable later with the '#states' 

$form['file'] = [
    '#title' =&gt; $this-&gt;t('File'),
    '#description' =&gt; $this-&gt;t('Upload a file from your computer'), 
    '#type' =&gt; 'file',
    '#default_value' =&gt; $entity-&gt;file-&gt;value,
];


But at the moment the field is displayed, but I have problems on saving the form. The file is attached, but not getting uploaded and the error says:


  Fatal error: Call to a member function getValue() on a non-object in
  C:\A
  plus\drupalsite\core\modules\file\src\Plugin\Validation\Constraint\FileValidationConstraintValidator.php
  on line 18
$form['File'] = array(
        '#type' =&gt; 'managed_file',
        '#title' =&gt; t('Choose  File'),
        '#upload_location' =&gt; 'public://MyFilesFolder/',
        '#default_value' =&gt; $entity-&gt;get('File')-&gt;value,
        '#description' =&gt; t('upload file'),
        '#states' =&gt; array(
          'visible' =&gt; array(
            ':input[name="File_type"]' =&gt; array('value' =&gt; t('Upload Your File')),
          ),
        ),
      );