1- import 'dart:html' as html;
21import 'dart:async' ;
2+ import 'dart:js_interop' ;
3+
4+ import 'package:web/web.dart' as web;
35
46import 'package:permission_handler_platform_interface/permission_handler_platform_interface.dart' ;
57
@@ -8,21 +10,21 @@ import 'package:permission_handler_platform_interface/permission_handler_platfor
810class WebDelegate {
911 /// Constructs a WebDelegate.
1012 WebDelegate (
11- html .MediaDevices ? devices,
12- html .Geolocation ? geolocation,
13- html .Permissions ? permissions,
13+ web .MediaDevices ? devices,
14+ web .Geolocation ? geolocation,
15+ web .Permissions ? permissions,
1416 ) : _devices = devices,
1517 _geolocation = geolocation,
1618 _htmlPermissions = permissions;
1719
1820 /// The html media devices object used to request camera and microphone permissions.
19- final html .MediaDevices ? _devices;
21+ final web .MediaDevices ? _devices;
2022
2123 /// The html geolocation object used to request location permission.
22- final html .Geolocation ? _geolocation;
24+ final web .Geolocation ? _geolocation;
2325
2426 /// The html permissions object used to check permission status.
25- final html .Permissions ? _htmlPermissions;
27+ final web .Permissions ? _htmlPermissions;
2628
2729 /// The permission name to request access to the camera.
2830 static const _microphonePermissionName = 'microphone' ;
@@ -58,8 +60,10 @@ class WebDelegate {
5860 }
5961 }
6062
61- Future <PermissionStatus > _permissionStatusState (String webPermissionName, html.Permissions ? permissions) async {
62- final webPermissionStatus = await permissions? .query ({'name' : webPermissionName});
63+ Future <PermissionStatus > _permissionStatusState (
64+ String webPermissionName, web.Permissions ? permissions) async {
65+ final webPermissionStatus =
66+ await permissions? .query ({'name' : webPermissionName}.toJSBox).toDart;
6367 return _toPermissionStatus (webPermissionStatus? .state);
6468 }
6569
@@ -69,7 +73,9 @@ class WebDelegate {
6973 }
7074
7175 try {
72- html.MediaStream mediaStream = await _devices! .getUserMedia ({'audio' : true });
76+ web.MediaStream ? mediaStream = await _devices
77+ ? .getUserMedia (web.MediaStreamConstraints (audio: true .toJS))
78+ .toDart;
7379
7480 // In browsers, calling [getUserMedia] will start the recording
7581 // automatically right after. This is undesired behavior as
@@ -78,13 +84,13 @@ class WebDelegate {
7884 // The manual stop action is then needed here for to stop the automatic
7985 // recording.
8086
81- if (mediaStream.active ?? false ) {
82- final audioTracks = mediaStream.getAudioTracks ();
87+ if (mediaStream? .active ?? false ) {
88+ final audioTracks = mediaStream? .getAudioTracks ().toDart ?? [] ;
8389 if (audioTracks.isNotEmpty) {
8490 audioTracks[0 ].stop ();
8591 }
8692 }
87- } on html. DomException {
93+ } on web. DOMException {
8894 return false ;
8995 }
9096
@@ -97,7 +103,9 @@ class WebDelegate {
97103 }
98104
99105 try {
100- html.MediaStream mediaStream = await _devices! .getUserMedia ({'video' : true });
106+ web.MediaStream ? mediaStream = await _devices
107+ ? .getUserMedia (web.MediaStreamConstraints (video: true .toJS))
108+ .toDart;
101109
102110 // In browsers, calling [getUserMedia] will start the recording
103111 // automatically right after. This is undesired behavior as
@@ -106,38 +114,51 @@ class WebDelegate {
106114 // The manual stop action is then needed here for to stop the automatic
107115 // recording.
108116
109- if (mediaStream.active ?? false ) {
110- final videoTracks = mediaStream.getVideoTracks ();
117+ if (mediaStream? .active ?? false ) {
118+ final videoTracks = mediaStream? .getVideoTracks ().toDart ?? [] ;
111119 if (videoTracks.isNotEmpty) {
112120 videoTracks[0 ].stop ();
113121 }
114122 }
115- } on html. DomException {
123+ } on web. DOMException {
116124 return false ;
117125 }
118126
119127 return true ;
120128 }
121129
122130 Future <bool > _requestNotificationPermission () async {
123- return html.Notification .requestPermission ().then ((permission) => permission == "granted" );
131+ return web.Notification .requestPermission ()
132+ .toDart
133+ .then ((permission) => (permission == "granted" .toJS));
124134 }
125135
126136 Future <bool > _requestLocationPermission () async {
137+ Completer <bool > completer = Completer <bool >();
127138 try {
128- return await _geolocation? .getCurrentPosition ().then ((value) => true ) ?? false ;
129- } on html.PositionError {
130- return false ;
139+ _geolocation? .getCurrentPosition (
140+ (JSAny _) {
141+ completer.complete (true );
142+ }.toJS,
143+ (JSAny _) {
144+ completer.complete (false );
145+ }.toJS,
146+ );
147+ } catch (_) {
148+ completer.complete (false );
131149 }
150+ return completer.future;
132151 }
133152
134- Future <PermissionStatus > _requestSingularPermission (Permission permission) async {
153+ Future <PermissionStatus > _requestSingularPermission (
154+ Permission permission) async {
135155 bool permissionGranted = switch (permission) {
136156 Permission .microphone => await _requestMicrophonePermission (),
137157 Permission .camera => await _requestCameraPermission (),
138158 Permission .notification => await _requestNotificationPermission (),
139159 Permission .location => await _requestLocationPermission (),
140- _ => throw UnsupportedError ('The ${permission .toString ()} permission is currently not supported on web.' )
160+ _ => throw UnsupportedError (
161+ 'The ${permission .toString ()} permission is currently not supported on web.' )
141162 };
142163
143164 if (! permissionGranted) {
@@ -150,12 +171,14 @@ class WebDelegate {
150171 /// they have not already been granted before.
151172 ///
152173 /// Returns a [Map] containing the status per requested [Permission] .
153- Future <Map <Permission , PermissionStatus >> requestPermissions (List <Permission > permissions) async {
174+ Future <Map <Permission , PermissionStatus >> requestPermissions (
175+ List <Permission > permissions) async {
154176 final Map <Permission , PermissionStatus > permissionStatusMap = {};
155177
156178 for (final permission in permissions) {
157179 try {
158- permissionStatusMap[permission] = await _requestSingularPermission (permission);
180+ permissionStatusMap[permission] =
181+ await _requestSingularPermission (permission);
159182 } on UnimplementedError {
160183 rethrow ;
161184 }
0 commit comments