Flutter compile time Platform check

Viewed 147

if you use dart:io's Platform.isIOS; Platform.isAndroid; or the kIsWeb constant is that a runtime or a compile time check? It is absolutely critical that it is a compile time check because I need the app to be minimal in size. I know you can do some complex/fancy checks with external packages and all that like described here, but I would love to keep it simple if else statements. Also can someone provide documentation or articles so that I can read up on it.

1 Answers

They differ. From Platform's documentation:

Information about the environment in which the current program is running.

However kIsWeb specifies:

A constant that is true if the application was compiled to run on the web.

According to this question the TreeShaker will remove code it can detect in compile time that is not reachable - so kIsWeb parts can effect the build while Platform cannot. Also note the answer mentions tree shaking will only be performed on Release versions, so make sure you test correctly.

Related