在构建 flutter 应用程序时,我收到此错误。
One or more plugins require a higher Android SDK version.
Fix this issue by adding the following to C:\flutter\projects\my_app\android\app\build.gradle:
android {
compileSdkVersion 33
...
}
Run Code Online (Sandbox Code Playgroud)
但是,在我的 build.gradle 中我有:
android {
compileSdkVersion flutter.compileSdkVersion
...
}
Run Code Online (Sandbox Code Playgroud)
但我无法找到定义 flutter.compileSdkVersion 的位置。知道这是在哪里定义的吗?我已经运行 flutter Upgrade 并且 flutter --version 返回 Flutter 3.0.5。
我正在使用 dart.io 包并尝试使用 Platform.localeName 检测系统的当前位置,如下所示
WidgetsFlutterBinding.ensureInitialized();
String defaultSystemLocale = Platform.localeName;
print('$defaultSystemLocale');
Run Code Online (Sandbox Code Playgroud)
但是当我在 Chrome (Web) 中运行该应用程序时,出现此错误
Error: Unsupported operation: Platform._localeName
at Object.throw_ [as throw] (http://localhost:62284/dart_sdk.js:5061:11)
at Function._localeName (http://localhost:62284/dart_sdk.js:54692:17)
at Function.localeName (http://localhost:62284/dart_sdk.js:54698:71)
at Function.get localeName [as localeName] (http://localhost:62284/dart_sdk.js:54574:27)
at main (http://localhost:62284/packages/testapp/main4.dart.lib.js:97:45)
Run Code Online (Sandbox Code Playgroud)
知道为什么吗?
我的 flutter 应用程序正在前台和后台使用电话包监听短信。在注册接收消息时,我使用了两种方法(一种用于前台,一种用于后台,根据电话包的要求,如下所示:
telephony.listenIncomingSms(onNewMessage: onMessage, onBackgroundMessage: onBackgroundMessage);
Run Code Online (Sandbox Code Playgroud)
但在这两个方法内部都调用了一个通用方法,该方法将最近收到的短信保存到共享首选项中,如下所示:
onMessage(SmsMessage message) async {
...
saveLastMsg(message);
}
onBackgroundMessage(SmsMessage message) async {
...
saveLastMsg(message);
}
saveLastMsg(SmsMessage message) async{
SharedPreferences sp = await SharedPreferences.getInstance();
sp.setString("lastsmsbody", message.body??'');
}
Run Code Online (Sandbox Code Playgroud)
在应用程序用户界面中,我像这样获取lastsmsbody:
SharedPreferences sp = await SharedPreferences.getInstance();
String body = sp.getString("lastsmsbody")??'';
Run Code Online (Sandbox Code Playgroud)
我在 UI 中显示该值。当应用程序在前台运行时,这工作正常。屏幕上的数据会随着消息的到来而更新。
问题是当应用程序被推入后台时。从日志中,我看到它仍然收到消息并且仍然将其保存在首选项中。但是,当我将应用程序带回到前台时,它无法获取应用程序处于后台时保存的最新消息。sp.getString("lastsmsbody") 获取应用程序位于前台时保存的旧消息。
我认为当应用程序位于 fg 和 bg 时,可能偏好不同。但是,如果我终止应用程序并重新启动,sp.getString("lastsmsbody") 将返回应用程序处于 bg 时保存的最新消息。
有人能告诉我这是怎么回事吗?
我不想追加也不想截断现有数据。我想覆盖现有数据。例如,以下代码使 test.txt 文件包含“hello”,但我希望该文件包含“hello6789”。
try(
FileWriter fw = new FileWriter("test.txt"); ){
fw.write("123456789");
}
try(
FileWriter fw = new FileWriter("test.txt"); ){
fw.write("hello");
}
Run Code Online (Sandbox Code Playgroud)
是否可以?