0

I am creating an application in Flutter. In this application, I need to call a library written in Java. Now, the library, written in Java, has a getter and a setter for a property. I want to call these getters and setters from Flutter/Dart, and we want to use it.

static const MethodChannel _channel = const MethodChannel('com.example.java-library');

static Future<String> get versionCode async {
  final String versionCode = await _channel.invokeMethod('getVersionCode');
  return versionCode;
}

static set versionCode(final String code) {
  _channel.invokeMethod('setVersionCode', code);
}

However, if I write code like the above, I will get a warning at runtime.

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.

How can I avoid the problem so that the warning does not appear?

1
  • I faced the same issue, do you have a solution?, this happens when im update dart sdk Commented May 1, 2021 at 21:31

1 Answer 1

1

Would be better to use methods like so:

static Future<String> getVersionCode() async {
  final String versionCode = await _channel.invokeMethod('getVersionCode');
  return versionCode;
}
static void setVersionCode(String code){
  _channel.invokeMethod('setVersionCode', code);
}

When you want to retrieve(use) the value:

Future<void> anyOtherPlace()async{
   final String value = await getVersionCode();
}
Sign up to request clarification or add additional context in comments.

1 Comment

You have to await for it, invokeMethod is async so you must await it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.