Cross Platform Interface Stub Pattern For Flutter

Fred Grott
4 min readMar 20, 2023

One of the Flutter cross-platform tricks is an interface stub pattern to deliver platform specific library implementations to the Flutter app. Let me show you how this works with how we access the platform specific shared preferences libraries.

The Shared Preferences Example

The shared preferences package is at:

But, generally, those libraries that have platform specific libraries do not include the pattern to use the right platform specific library. So I am going to use the shared preferences as an example.

So first we need a stub:

And the interface it is using is:

--

--