Accessing Flash messages in Middleware

I’m using CakePHP 4. In Middleware I would like to access Flash message, but it’s always empty.

In Middleware I’m using this code:

$session = $request->getSession();
debug($session->read('Flash'));

As I’m aware Flash messages are saved in session. I’m doing something wrong? On site I see flash message…

Tnx

What are you trying to achieve? according to this you can do something like this:

if ($request instanceof ServerRequest) {
$request->getFlash()->error(__('Error'));
}

I use the if clause so I dont get annoyed by the vscode Intellisense

Also, according to this you can start a new stack with:

$this->Flash->success('The user has been saved', ['clear' => true]);