javascript_channel.dart 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import 'package:meta/meta.dart';
  2. import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
  3. final RegExp _validChannelNames = RegExp('^[a-zA-Z_][a-zA-Z0-9]*\$');
  4. /// A named channel for receiving messaged from JavaScript code running inside a web view.
  5. class JavascriptChannel {
  6. /// Constructs a Javascript channel.
  7. ///
  8. /// The parameters `name` and `onMessageReceived` must not be null.
  9. JavascriptChannel({
  10. @required this.name,
  11. @required this.onMessageReceived,
  12. }) : assert(name != null),
  13. assert(onMessageReceived != null),
  14. assert(_validChannelNames.hasMatch(name));
  15. /// The channel's name.
  16. ///
  17. /// Passing this channel object as part of a [WebView.javascriptChannels] adds a channel object to
  18. /// the Javascript window object's property named `name`.
  19. ///
  20. /// The name must start with a letter or underscore(_), followed by any combination of those
  21. /// characters plus digits.
  22. ///
  23. /// Note that any JavaScript existing `window` property with this name will be overriden.
  24. ///
  25. /// See also [WebView.javascriptChannels] for more details on the channel registration mechanism.
  26. final String name;
  27. /// A callback that's invoked when a message is received through the channel.
  28. final JavascriptMessageHandler onMessageReceived;
  29. }
  30. /// Callback type for handling messages sent from Javascript running in a web view.
  31. typedef void JavascriptMessageHandler(JavascriptMessage message);