@@ -34,7 +34,8 @@ class WebDelegate {
34
34
static const _notificationsPermissionName = 'notifications' ;
35
35
36
36
/// The permission name to request access to the user's location.
37
- static const _locationPermissionName = 'location' ;
37
+ /// https://developer.mozilla.org/en-US/docs/Web/API/Permissions/query#name
38
+ static const _locationPermissionName = 'geolocation' ;
38
39
39
40
/// The status indicates that permission has been granted by the user.
40
41
static const _grantedPermissionStatus = 'granted' ;
@@ -57,18 +58,18 @@ class WebDelegate {
57
58
}
58
59
}
59
60
60
- Future <PermissionStatus > _permissionStatusState (
61
- String webPermissionName, html.Permissions ? permissions) async {
62
- final webPermissionStatus =
63
- await permissions? .query ({'name' : webPermissionName});
61
+ Future <PermissionStatus > _permissionStatusState (String webPermissionName, html.Permissions ? permissions) async {
62
+ final webPermissionStatus = await permissions? .query ({'name' : webPermissionName});
64
63
return _toPermissionStatus (webPermissionStatus? .state);
65
64
}
66
65
67
- Future <bool > _requestMicrophonePermission (html.MediaDevices devices) async {
68
- html.MediaStream ? mediaStream;
66
+ Future <bool > _requestMicrophonePermission () async {
67
+ if (_devices == null ) {
68
+ return false ;
69
+ }
69
70
70
71
try {
71
- mediaStream = await devices .getUserMedia ({'audio' : true });
72
+ html. MediaStream mediaStream = await _devices ! .getUserMedia ({'audio' : true });
72
73
73
74
// In browsers, calling [getUserMedia] will start the recording
74
75
// automatically right after. This is undesired behavior as
@@ -77,7 +78,7 @@ class WebDelegate {
77
78
// The manual stop action is then needed here for to stop the automatic
78
79
// recording.
79
80
80
- if (mediaStream.active! ) {
81
+ if (mediaStream.active ?? false ) {
81
82
final audioTracks = mediaStream.getAudioTracks ();
82
83
if (audioTracks.isNotEmpty) {
83
84
audioTracks[0 ].stop ();
@@ -90,11 +91,13 @@ class WebDelegate {
90
91
return true ;
91
92
}
92
93
93
- Future <bool > _requestCameraPermission (html.MediaDevices devices) async {
94
- html.MediaStream ? mediaStream;
94
+ Future <bool > _requestCameraPermission () async {
95
+ if (_devices == null ) {
96
+ return false ;
97
+ }
95
98
96
99
try {
97
- mediaStream = await devices .getUserMedia ({'video' : true });
100
+ html. MediaStream mediaStream = await _devices ! .getUserMedia ({'video' : true });
98
101
99
102
// In browsers, calling [getUserMedia] will start the recording
100
103
// automatically right after. This is undesired behavior as
@@ -103,7 +106,7 @@ class WebDelegate {
103
106
// The manual stop action is then needed here for to stop the automatic
104
107
// recording.
105
108
106
- if (mediaStream.active! ) {
109
+ if (mediaStream.active ?? false ) {
107
110
final videoTracks = mediaStream.getVideoTracks ();
108
111
if (videoTracks.isNotEmpty) {
109
112
videoTracks[0 ].stop ();
@@ -117,45 +120,25 @@ class WebDelegate {
117
120
}
118
121
119
122
Future <bool > _requestNotificationPermission () async {
120
- bool granted = false ;
121
- html.Notification .requestPermission ().then ((permission) => {
122
- if (permission == "granted" ) {granted = true }
123
- });
124
-
125
- return granted;
123
+ return html.Notification .requestPermission ().then ((permission) => permission == "granted" );
126
124
}
127
125
128
- Future <bool > _requestLocationPermission (html. Geolocation geolocation ) async {
126
+ Future <bool > _requestLocationPermission () async {
129
127
try {
130
- await geolocation.getCurrentPosition ();
131
- return true ;
128
+ return await _geolocation? .getCurrentPosition ().then ((value) => true ) ?? false ;
132
129
} on html.PositionError {
133
130
return false ;
134
131
}
135
132
}
136
133
137
- Future <PermissionStatus > _requestSingularPermission (
138
- Permission permission) async {
139
- bool permissionGranted = false ;
140
-
141
- switch (permission) {
142
- case Permission .microphone:
143
- permissionGranted = await _requestMicrophonePermission (_devices! );
144
- break ;
145
- case Permission .camera:
146
- permissionGranted = await _requestCameraPermission (_devices! );
147
- break ;
148
- case Permission .notification:
149
- permissionGranted = await _requestNotificationPermission ();
150
- break ;
151
- case Permission .location:
152
- permissionGranted = await _requestLocationPermission (_geolocation! );
153
- break ;
154
- default :
155
- throw UnsupportedError (
156
- 'The ${permission .toString ()} permission is currently not supported on web.' ,
157
- );
158
- }
134
+ Future <PermissionStatus > _requestSingularPermission (Permission permission) async {
135
+ bool permissionGranted = switch (permission) {
136
+ Permission .microphone => await _requestMicrophonePermission (),
137
+ Permission .camera => await _requestCameraPermission (),
138
+ Permission .notification => await _requestNotificationPermission (),
139
+ Permission .location => await _requestLocationPermission (),
140
+ _ => throw UnsupportedError ('The ${permission .toString ()} permission is currently not supported on web.' )
141
+ };
159
142
160
143
if (! permissionGranted) {
161
144
return PermissionStatus .permanentlyDenied;
@@ -167,14 +150,12 @@ class WebDelegate {
167
150
/// they have not already been granted before.
168
151
///
169
152
/// Returns a [Map] containing the status per requested [Permission] .
170
- Future <Map <Permission , PermissionStatus >> requestPermissions (
171
- List <Permission > permissions) async {
153
+ Future <Map <Permission , PermissionStatus >> requestPermissions (List <Permission > permissions) async {
172
154
final Map <Permission , PermissionStatus > permissionStatusMap = {};
173
155
174
156
for (final permission in permissions) {
175
157
try {
176
- permissionStatusMap[permission] =
177
- await _requestSingularPermission (permission);
158
+ permissionStatusMap[permission] = await _requestSingularPermission (permission);
178
159
} on UnimplementedError {
179
160
rethrow ;
180
161
}
0 commit comments