See example folder for some examples of Flutter and Dart Chrome extensions
Tips to build Chrome extensions with Flutter
Here are some personal tips to build Chrome extension using the Flutter UI framework.
Develop the app using Flutter Desktop
In order to develop in a comfortable environment with hot-reload,
most of the app (the UI part) should be developed using Flutter desktop.
This requires an abstraction layer between the UI and the chrome_extension APIs.
In the Desktop entry point, a fake implementation of this abstraction layer is used, like this:
// lib/main_desktop.dartvoidmain() {
// Inject a fake service that doesn't use the real chrome_extension package.var service = FakeBookmarkService();
runApp(MyExtensionPopup(service));
}
abstractclassBookmarkService{
Future<List<Bookmark>> getBookmarks();
}
classFakeBookmarkServiceimplementsBookmarkService{
@overrideFuture<List<Bookmark>> getBookmarks() async => [Bookmark()];
}
Launch this entry point in desktop with flutter run -t lib/main_desktop.dart -d macos|windows|linux
And the real entry point (the one used in the actual compiled extension) looks like:
// lib/main.dartvoidmain() {
var service = ChromeBookmarkService();
runApp(MyExtensionPopup(service));
}
classChromeBookmarkServiceimplementsBookmarkService{
@overrideFuture<List<Bookmark>> getBookmarks() async {
// Real implementation using chrome.bookmarksreturn (await chrome.bookmarks.getTree()).map(Bookmark.new).toList();
}
}