为Android编译时是否会删除iOS的dart代码?

Roc*_*ice 5 android compilation dart flutter

我正在使用 flutter 为 iOS 和 Android 平台编写应用程序。有些功能不一样。

例如:

if (Platform.isIOS) {
    int onlyForiOS = 10;
    onlyForiOS++;
    print("$onlyForiOS");
}
else if (Platform.isAndroid){
    int onlyForAndroid = 20;
    onlyForAndroid++;
    print("$onlyForAndroid");
}
Run Code Online (Sandbox Code Playgroud)

当我为Android平台构建时,iOS的代码会被编译成二进制文件吗?或者它们只是为了优化而被删除?出于安全原因,我不希望任何 iOS 代码出现在 Android 二进制文件中。

Rém*_*let 4

这取决于您正在评估的表达式。

Dart tree-shaking 基于常量变量。因此,以下内容将被摇树:

const foo = false;
if (foo) {
  // will be removed on release builds
}
Run Code Online (Sandbox Code Playgroud)

但这个例子不会:

final foo = false;
if (foo) {
  // foo is not a const, therefore this if is not tree-shaked
}
Run Code Online (Sandbox Code Playgroud)

现在如果我们看一下 的实现Platform.isAndroid,我们可以看到它不是一个常量,而是一个 getter。

因此我们可以推断它if (Platform.isAndroid)不会被 tree-shaking 。