dart-lang / io

Utilities for the Dart VM's dart:io.
https://pub.dev/packages/io
BSD 3-Clause "New" or "Revised" License
49 stars 15 forks source link

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

Open AbhishekDoshi26 opened 1 year ago

AbhishekDoshi26 commented 1 year ago

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:

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.

stuartmorgan commented 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.

hemangjoshi37a commented 1 year ago

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

hemangjoshi37a commented 1 year ago

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