google-chromeflutter-webgoogle-chrome-app

Flutter for web - how use Chrome Api?


I am creating my first Flutter app for web. I need using Chrome api. For example I need to use it:

chrome.runtime.onInstalled.addListener

   or

   chrome.tabs...

but unfortunately I did not find any information about it.

Is it possible?


Solution

  • Yes, the chrome.* APIs are JavaScript just like all the other web APIs, so you can use Dart's JS support.

    That means using Dart's js library.

    For example, you could bind a function something like this (untested, without any type annotation, just an example)

    @JS('chrome.runtime.onInstalled.addListener')
    external void addInstalledListener(Function callback);
    

    Edit: If you'd rather pull in a dependency than roll your own, you can use something like chrome.dart.

    import 'package:chrome/chrome_app.dart' as chrome;
    
    void main() {
      chrome.runtime.getPlatformInfo().then((Map m) {
        print(m.toString());
      });
    }