Java 如何使用flatter/Dart编写异步getter?

Java 如何使用flatter/Dart编写异步getter?,java,flutter,asynchronous,dart,Java,Flutter,Asynchronous,Dart,我正在颤振中创建一个应用程序。 在这个应用程序中,我需要调用一个用Java编写的库。 现在,用Java编写的库有一个属性的getter和setter。 我想把这些来自Flutter/Dart的getter和setter称为,我们想使用它 static const MethodChannel\u channel=const MethodChannel('com.example.java library'); 静态未来获取版本代码异步{ 最终字符串versionCode=await_channel.

我正在颤振中创建一个应用程序。 在这个应用程序中,我需要调用一个用Java编写的库。 现在,用Java编写的库有一个属性的getter和setter。 我想把这些来自Flutter/Dart的getter和setter称为,我们想使用它

static const MethodChannel\u channel=const MethodChannel('com.example.java library');
静态未来获取版本代码异步{
最终字符串versionCode=await_channel.invokeMethod('getVersionCode');
返回版本代码;
}
静态设置版本代码(最终字符串代码){
_channel.invokeMethod('setVersionCode',代码);
}
但是,如果我像上面那样编写代码,我将在运行时收到警告

The return type of getter 'versionCode' is 'Future<String>' which isn't assignable to the type 'String' of its setter 'versionCode'.
Try changing the types so that they are compatible.
getter'versionCode'的返回类型为'Future',不能分配给其setter'versionCode'的类型'String'。
尝试更改类型以使其兼容。

如何避免出现警告?最好使用以下方法:

static Future<String> getVersionCode() async {
  final String versionCode = await _channel.invokeMethod('getVersionCode');
  return versionCode;
}
要检索(使用)该值时:

Future<void> anyOtherPlace()async{
   final String value = await getVersionCode();
}
Future anyOtherPlace()异步{
最终字符串值=等待getVersionCode();
}

您必须等待,invokeMethod是异步的,因此您必须等待。我遇到了同样的问题,您有解决方案吗?当im更新dart sdk时会发生这种情况
Future<void> anyOtherPlace()async{
   final String value = await getVersionCode();
}