wjohnson
wjohnson

Reputation: 761

Can one specify a Dart interface that may be implemented by classes with either sync or async methods?

For a Dart component that uses remote services, I started by defining a Dart service interface as an abstract class (e.g. abstract class XService) and a mock class (class XServiceMock implements XService) with synchronous methods that implemented the service API which I used for testing the component. I am now starting to implement the real proxy (class XServiceImpl implements XService) and several of its methods use the ‘await/async’ language features. The ‘await’ requires that the containing method be annotated with async which requires a return type of Future<Something> and puts it in conflict with the method signatures in XService. I want to be able to inject either the mock proxy or the real one without modifying the component source code. Can this be done and how?

XService (api)
XServiceMock implements XService (sync methods only)
XServiceImpl implements XService (mixture of async and sync methods)

Upvotes: 2

Views: 888

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657871

This is not possible. What you can do is specifying an async interface and returning a future from synchronous code.

return new Future.value(someReturnValue);

Upvotes: 3

Related Questions