io icon indicating copy to clipboard operation
io copied to clipboard

[Platform.is*] Using of Platform.isAndroid or Platform.isIOS should either be deprecated or documentation should be clear

Open AbhishekDoshi26 opened this issue 2 years ago • 3 comments

We all know that using Platform.isAndroid and Platform.isIOS crashes on web because these constants lie inside dart: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:

  • Deprecate Platform.is* so that we don't make silly mistakes
  • Properly mention in the documentation that this should not be used in projects with web implementations.

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.

AbhishekDoshi26 avatar Jan 04 '23 16:01 AbhishekDoshi26

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.

stuartmorgan-g avatar Jan 04 '23 20:01 stuartmorgan-g

can anyone please give me any example on how to check target platform using TargetPlatform.android instead of Platform.isAndroid . Thanks.

hemangjoshi37a avatar Jan 20 '23 09:01 hemangjoshi37a

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.
}

hemangjoshi37a avatar Jan 20 '23 09:01 hemangjoshi37a