I have tried the below code block in @Before and nothing happens the test passed with no error but device mode did not change to dark
((JavascriptExecutor)driver).executeScript("mobile:setAppearance", "{style: 'dark'}");
This command is an alias to the xcrun simctl ui
command which is only applicable to the simulators.
The only way to do this on a real device is to open Settings -> Display & Brightness and click on Dark mode. You can see some details on this topic