Skip to content

Instantly share code, notes, and snippets.

@ditman
Last active October 30, 2019 19:16
Show Gist options
  • Save ditman/c8095fa64d7ab880765861d74a74aa05 to your computer and use it in GitHub Desktop.
Save ditman/c8095fa64d7ab880765861d74a74aa05 to your computer and use it in GitHub Desktop.
Flutter plugins with implements PlatformInterface
// Provided by flutter/plugins or similar
class Plugin {
@override
void noSuchMethod(Invocation i) {
throw Exception('Plugin does not implement method ${i.memberName}');
}
}
class MockPlugin extends Plugin {
@override
void noSuchMethod(Invocation i) {
print('Plugin does not implement method ${i.memberName} (but it didn\'t throw)');
}
}
// Provided by each plugin
abstract class PluginInterface {
void methodOne();
void methodTwo();
}
// Provided by each implementation (note: missing methodTwo)
class MyImplementation extends Plugin implements PluginInterface {
void methodOne() {
print("hello");
}
}
// For tests...
class MyMockImplementation extends MockPlugin implements PluginInterface {
}
void main() {
final MyMockImplementation myMockImpl = MyMockImplementation();
myMockImpl.methodOne(); // Print but not throw
myMockImpl.methodTwo();
final MyImplementation myImpl = MyImplementation();
myImpl.methodOne();
myImpl.methodTwo(); // This throws
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment