Last active
October 30, 2019 19:16
-
-
Save ditman/c8095fa64d7ab880765861d74a74aa05 to your computer and use it in GitHub Desktop.
Flutter plugins with implements PlatformInterface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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