maybeWhen<R> static method
R
maybeWhen<R>({
- required R orElse(),
- R android()?,
- R fuchsia()?,
- R iOS()?,
- R linux()?,
- R macOS()?,
- R web()?,
- R windows()?,
Invokes the given function based on the current platform.
Example usage:
FunctionalPlatform.maybeWhen(
android: () => print('Running on Android'),
iOS: () => print('Running on iOS'),
orElse: () => print('Running on other platform'),
);
The orElse
function is required and will be called if no specific
platform function is provided or if the current platform does not match
any of the provided platform functions.
The optional platform-specific functions (android
, fuchsia
, iOS
,
linux
, macOS
, web
, windows
) will be called if the current platform
matches the respective platform.
Returns the result of the invoked function.
Implementation
static R maybeWhen<R>({
required R Function() orElse,
R Function()? android,
R Function()? fuchsia,
R Function()? iOS,
R Function()? linux,
R Function()? macOS,
R Function()? web,
R Function()? windows,
}) {
if (kIsWeb) return web?.call() ?? orElse(); // IO is not available on Web.
switch (defaultTargetPlatform) {
case TargetPlatform.android:
return android?.call() ?? orElse();
case TargetPlatform.iOS:
return iOS?.call() ?? orElse();
case TargetPlatform.macOS:
return macOS?.call() ?? orElse();
case TargetPlatform.windows:
return windows?.call() ?? orElse();
case TargetPlatform.linux:
return linux?.call() ?? orElse();
case TargetPlatform.fuchsia:
return fuchsia?.call() ?? orElse();
}
}