Error on create / save page (in Panel)

Hello,

Kirby v.3.8.1.1
Context: Add a new page
Error: Call to a member function keys() on null

This only occurs on a page with a specific template.
On this page the user needs to upload a .csv file to create virtual subspages.
But I cannot create any page.
I think something in the page Model is at the origin of the problem.
The page model contains:

     public function children()
    {
        // check if a CSV file has been imported (datafile field)
        if ($this->datafile()->isNotEmpty()) {
            $datafile = $this->datafile()->toFile()->root();
            $csv = csv($datafile, ';');

            $children = array_map(function ($logiciel) {
                return [
                    'slug'       => Str::slug($logiciel['logiciel']),
                    'template'   => 'logiciel',
                    'num'        => 0,
                    'content'    => [
                        'logiciel'    => mb_strtoupper(trim($logiciel['logiciel'])),
                        'has'         => trim($logiciel['has']),
                        'editeur'     => mb_strtoupper(trim($logiciel['editeur'])),
                        'description' => trim($logiciel['description']),
                        'adresse'     => trim($logiciel['adresse']),
                        'cp'          => trim($logiciel['cp']),
                        'localite'    => mb_strtoupper(trim($logiciel['localite'])),
                        'tel'         => trim($logiciel['tel']),
                        'web'         => trim($logiciel['web']),
                        'logo'        => trim($logiciel['logo']) ?? '',
                        'categorie'   => trim($logiciel['categorie']),
                        'poids'       => trim($logiciel['poids']),
                        'datafile'    => $this->datafile()->isNotEmpty(),
                    ]
                ];
            }, $csv);

            return Pages::factory($children, $this);
        }
    }

What am I doing wrong?

I just created a page by manually adding the .txt file of the content.
It is then possible to access this page from the Panel, but any changes cannot be saved, because the same error occurs.
If I upload a .csv file to generate the virtual subpages, the error no longer occurs.
So it seems that the datafile() field test is involved in the problem, I don’t see how to get around it.

I solved my problem by using a dummy .csv file to be passed to the csv() function.

public function children()
   {
      if ($this->datafile()->isNotEmpty()) {
         $datafile = $this->datafile()->toFile()->root();
      } else {
         $datafile = $this->kirby()->root('assets') . 'dummy.csv';
         // throw new Exception('> ' . $datafile);
      }
      $csv = csv($datafile, ';');
...

I don’t think this is needed, you can just return an empty array. Your file logic needs to be amended a bit as well, because as it is, it is error prone.

public function children()
    {
        // check if a CSV file has been imported (datafile field)
        $datafile =  $this->datafile()->toFile();
        $children = [];
        if ($datafile) {
           $csv = csv($datafile->root(), ';');

            $children = array_map(function ($logiciel) {
                return [
                    'slug'       => Str::slug($logiciel['logiciel']),
                    'template'   => 'logiciel',
                    'num'        => 0,
                    'content'    => [
                        'logiciel'    => mb_strtoupper(trim($logiciel['logiciel'])),
                        'has'         => trim($logiciel['has']),
                        'editeur'     => mb_strtoupper(trim($logiciel['editeur'])),
                        'description' => trim($logiciel['description']),
                        'adresse'     => trim($logiciel['adresse']),
                        'cp'          => trim($logiciel['cp']),
                        'localite'    => mb_strtoupper(trim($logiciel['localite'])),
                        'tel'         => trim($logiciel['tel']),
                        'web'         => trim($logiciel['web']),
                        'logo'        => trim($logiciel['logo']) ?? '',
                        'categorie'   => trim($logiciel['categorie']),
                        'poids'       => trim($logiciel['poids']),
                        'datafile'    => $this->datafile()->isNotEmpty(),
                    ]
                ];
            }, $csv);
        }
     
       return Pages::factory($children, $this);
        
    }

Yes texnixe, you are absolutely right.
Thanks to point it