我的调试 APK 工作正常,但从命令构建后发布 APK 无法工作flutter build apk。这里真正的问题是什么?
在调试模式下,任何全局变量或方法都可以完美工作,但在发布模式下,仅编译本机代码。因此,假设我们收到一些未格式化的文本,我们想要对其进行格式化并返回,因此如果您有一个全局函数来格式化文本(如下所示),它将在调试模式下正常工作,但在发布模式下可能会导致问题。
具有全局函数的代码。
// Global Function
String formatText(String unformattedText){
// ....
return formattedText;
}
Widget _showFormattedText(String unformattedText) {
final fd = formatText(unformattedText);
return Text(fd);
}
Run Code Online (Sandbox Code Playgroud)
相反,我们应该遵循最佳实践,并将所有内容包装在全球范围内存在的类中。
// Code with class method.
class CustomFunctions{
static String formatText(String unformattedText){
// ....
return formattedText;
}
}
Widget _showFormattedText(String unformattedText) {
final fd = CustomFunctions.formatText(unformattedText);
return Text(fd);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2705 次 |
| 最近记录: |