Open AbhishekDoshi26 opened 1 year ago
We already have an alternative i.e. to lay down the if-else such that it checks for web first.
How is that an alternative to using Platform
checks? The rest of the conditional uses it.
Or, we can also use
defaultTargetPlatform == TargetPlatform.android
as this works on all platforms.
Those don't check the same thing. The platform Flutter behavior is targeting doesn't have to be the same as the platform that you are running on.
can anyone please give me any example on how to check target platform using TargetPlatform.android
instead of Platform.isAndroid
.
Thanks.
We can also use
import 'package:flutter/foundation.dart' show kIsWeb;
if (kIsWeb) {
// running on the web!
} else {
// NOT running on the web! You can check for additional platforms here.
}
We all know that using
Platform.isAndroid
andPlatform.isIOS
crashes on web because these constants lie insidedart:io
which is not supported for web and many times no one really checks which file was imported due to the usage of something.But, many less experienced developers keep on using these for multi-platform apps because they are habitual to use it. We already have an alternative i.e. to lay down the if-else such that it checks for web first. Or, we can also use
defaultTargetPlatform == TargetPlatform.android
as this works on all platforms.I propose either of the following:
This will really help developers to not make silly mistakes of using
Platform.is*
in multi-platform projects.Note: Opened similar issue in flutter repo too.