Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
[various] Enable avoid_dynamic_calls (#6834)
Browse files Browse the repository at this point in the history
* Enable the option

* Fix camera

* Fix webview

* Remove unnecessary 'call's from camera tests

* Fix maps

* Fix sign-in

* fix image_picker

* Fix IAP

* Fix shared_preferences

* Fix url_launcher_android

* Version bumps

* Fix tool

* Re-apply webview test fix

* Re-bump versions

* Fix one new tool issue
  • Loading branch information
stuartmorgan authored Jan 10, 2023
1 parent fb40581 commit 9fdc899
Show file tree
Hide file tree
Showing 69 changed files with 565 additions and 330 deletions.
2 changes: 1 addition & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ linter:
# - avoid_catching_errors # blocked on https://github.com/dart-lang/linter/issues/3023
- avoid_classes_with_only_static_members
- avoid_double_and_int_checks
# - avoid_dynamic_calls # LOCAL CHANGE - Needs to be enabled and violations fixed.
- avoid_dynamic_calls
- avoid_empty_else
- avoid_equals_and_hash_code_on_mutable_classes
- avoid_escaping_inner_quotes
Expand Down
4 changes: 4 additions & 0 deletions packages/camera/camera_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.10.2+1

* Updates code for stricter lint checks.

## 0.10.2

* Remove usage of deprecated quiver Optional type.
Expand Down
41 changes: 26 additions & 15 deletions packages/camera/camera_android/lib/src/android_camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,9 @@ class AndroidCamera extends CameraPlatform {
Future<dynamic> _handleDeviceMethodCall(MethodCall call) async {
switch (call.method) {
case 'orientation_changed':
final Map<String, Object?> arguments = _getArgumentDictionary(call);
_deviceEventStreamController.add(DeviceOrientationChangedEvent(
deserializeDeviceOrientation(
call.arguments['orientation']! as String)));
deserializeDeviceOrientation(arguments['orientation']! as String)));
break;
default:
throw MissingPluginException();
Expand All @@ -566,21 +566,23 @@ class AndroidCamera extends CameraPlatform {
Future<dynamic> handleCameraMethodCall(MethodCall call, int cameraId) async {
switch (call.method) {
case 'initialized':
final Map<String, Object?> arguments = _getArgumentDictionary(call);
cameraEventStreamController.add(CameraInitializedEvent(
cameraId,
call.arguments['previewWidth']! as double,
call.arguments['previewHeight']! as double,
deserializeExposureMode(call.arguments['exposureMode']! as String),
call.arguments['exposurePointSupported']! as bool,
deserializeFocusMode(call.arguments['focusMode']! as String),
call.arguments['focusPointSupported']! as bool,
arguments['previewWidth']! as double,
arguments['previewHeight']! as double,
deserializeExposureMode(arguments['exposureMode']! as String),
arguments['exposurePointSupported']! as bool,
deserializeFocusMode(arguments['focusMode']! as String),
arguments['focusPointSupported']! as bool,
));
break;
case 'resolution_changed':
final Map<String, Object?> arguments = _getArgumentDictionary(call);
cameraEventStreamController.add(CameraResolutionChangedEvent(
cameraId,
call.arguments['captureWidth']! as double,
call.arguments['captureHeight']! as double,
arguments['captureWidth']! as double,
arguments['captureHeight']! as double,
));
break;
case 'camera_closing':
Expand All @@ -589,23 +591,32 @@ class AndroidCamera extends CameraPlatform {
));
break;
case 'video_recorded':
final Map<String, Object?> arguments = _getArgumentDictionary(call);
cameraEventStreamController.add(VideoRecordedEvent(
cameraId,
XFile(call.arguments['path']! as String),
call.arguments['maxVideoDuration'] != null
? Duration(
milliseconds: call.arguments['maxVideoDuration']! as int)
XFile(arguments['path']! as String),
arguments['maxVideoDuration'] != null
? Duration(milliseconds: arguments['maxVideoDuration']! as int)
: null,
));
break;
case 'error':
final Map<String, Object?> arguments = _getArgumentDictionary(call);
cameraEventStreamController.add(CameraErrorEvent(
cameraId,
call.arguments['description']! as String,
arguments['description']! as String,
));
break;
default:
throw MissingPluginException();
}
}

/// Returns the arguments of [call] as typed string-keyed Map.
///
/// This does not do any type validation, so is only safe to call if the
/// arguments are known to be a map.
Map<String, Object?> _getArgumentDictionary(MethodCall call) {
return (call.arguments as Map<Object?, Object?>).cast<String, Object?>();
}
}
2 changes: 1 addition & 1 deletion packages/camera/camera_android/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: camera_android
description: Android implementation of the camera plugin.
repository: https://github.com/flutter/plugins/tree/main/packages/camera/camera_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
version: 0.10.2
version: 0.10.2+1

environment:
sdk: ">=2.14.0 <3.0.0"
Expand Down
8 changes: 5 additions & 3 deletions packages/camera/camera_android/test/android_camera_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -504,11 +504,13 @@ void main() {
]);
expect(cameras.length, returnData.length);
for (int i = 0; i < returnData.length; i++) {
final Map<String, Object?> typedData =
(returnData[i] as Map<dynamic, dynamic>).cast<String, Object?>();
final CameraDescription cameraDescription = CameraDescription(
name: returnData[i]['name']! as String,
name: typedData['name']! as String,
lensDirection:
parseCameraLensDirection(returnData[i]['lensFacing']! as String),
sensorOrientation: returnData[i]['sensorOrientation']! as int,
parseCameraLensDirection(typedData['lensFacing']! as String),
sensorOrientation: typedData['sensorOrientation']! as int,
);
expect(cameras[i], cameraDescription);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,26 @@ class InstanceManager {
/// This method also expects the host `InstanceManager` to have a strong
/// reference to the instance the identifier is associated with.
T? getInstanceWithWeakReference<T extends Object>(int identifier) {
final Object? weakInstance = _weakInstances[identifier]?.target;
final T? weakInstance = _weakInstances[identifier]?.target as T?;

if (weakInstance == null) {
final Object? strongInstance = _strongInstances[identifier];
final T? strongInstance = _strongInstances[identifier] as T?;
if (strongInstance != null) {
final Object copy =
_copyCallbacks[identifier]!(strongInstance)! as Object;
// This cast is safe since it matches the argument type for
// _addInstanceWithIdentifier, which is the only place _copyCallbacks
// is populated.
final T Function(T) copyCallback =
_copyCallbacks[identifier]! as T Function(T);
final T copy = copyCallback(strongInstance);
_identifiers[copy] = identifier;
_weakInstances[identifier] = WeakReference<Object>(copy);
_weakInstances[identifier] = WeakReference<T>(copy);
_finalizer.attach(copy, identifier, detach: copy);
return copy as T;
return copy;
}
return strongInstance as T?;
return strongInstance;
}

return weakInstance as T;
return weakInstance;
}

/// Retrieves the identifier associated with instance.
Expand Down
4 changes: 4 additions & 0 deletions packages/camera/camera_avfoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.9.10+1

* Updates code for stricter lint checks.

## 0.9.10

* Remove usage of deprecated quiver Optional type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,9 +555,9 @@ class AVFoundationCamera extends CameraPlatform {
Future<dynamic> _handleDeviceMethodCall(MethodCall call) async {
switch (call.method) {
case 'orientation_changed':
final Map<String, Object?> arguments = _getArgumentDictionary(call);
_deviceEventStreamController.add(DeviceOrientationChangedEvent(
deserializeDeviceOrientation(
call.arguments['orientation']! as String)));
deserializeDeviceOrientation(arguments['orientation']! as String)));
break;
default:
throw MissingPluginException();
Expand All @@ -572,21 +572,23 @@ class AVFoundationCamera extends CameraPlatform {
Future<dynamic> handleCameraMethodCall(MethodCall call, int cameraId) async {
switch (call.method) {
case 'initialized':
final Map<String, Object?> arguments = _getArgumentDictionary(call);
cameraEventStreamController.add(CameraInitializedEvent(
cameraId,
call.arguments['previewWidth']! as double,
call.arguments['previewHeight']! as double,
deserializeExposureMode(call.arguments['exposureMode']! as String),
call.arguments['exposurePointSupported']! as bool,
deserializeFocusMode(call.arguments['focusMode']! as String),
call.arguments['focusPointSupported']! as bool,
arguments['previewWidth']! as double,
arguments['previewHeight']! as double,
deserializeExposureMode(arguments['exposureMode']! as String),
arguments['exposurePointSupported']! as bool,
deserializeFocusMode(arguments['focusMode']! as String),
arguments['focusPointSupported']! as bool,
));
break;
case 'resolution_changed':
final Map<String, Object?> arguments = _getArgumentDictionary(call);
cameraEventStreamController.add(CameraResolutionChangedEvent(
cameraId,
call.arguments['captureWidth']! as double,
call.arguments['captureHeight']! as double,
arguments['captureWidth']! as double,
arguments['captureHeight']! as double,
));
break;
case 'camera_closing':
Expand All @@ -595,23 +597,32 @@ class AVFoundationCamera extends CameraPlatform {
));
break;
case 'video_recorded':
final Map<String, Object?> arguments = _getArgumentDictionary(call);
cameraEventStreamController.add(VideoRecordedEvent(
cameraId,
XFile(call.arguments['path']! as String),
call.arguments['maxVideoDuration'] != null
? Duration(
milliseconds: call.arguments['maxVideoDuration']! as int)
XFile(arguments['path']! as String),
arguments['maxVideoDuration'] != null
? Duration(milliseconds: arguments['maxVideoDuration']! as int)
: null,
));
break;
case 'error':
final Map<String, Object?> arguments = _getArgumentDictionary(call);
cameraEventStreamController.add(CameraErrorEvent(
cameraId,
call.arguments['description']! as String,
arguments['description']! as String,
));
break;
default:
throw MissingPluginException();
}
}

/// Returns the arguments of [call] as typed string-keyed Map.
///
/// This does not do any type validation, so is only safe to call if the
/// arguments are known to be a map.
Map<String, Object?> _getArgumentDictionary(MethodCall call) {
return (call.arguments as Map<Object?, Object?>).cast<String, Object?>();
}
}
2 changes: 1 addition & 1 deletion packages/camera/camera_avfoundation/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: camera_avfoundation
description: iOS implementation of the camera plugin.
repository: https://github.com/flutter/plugins/tree/main/packages/camera/camera_avfoundation
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
version: 0.9.10
version: 0.9.10+1

environment:
sdk: ">=2.14.0 <3.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,9 @@ void main() {
test('Should fetch CameraDescription instances for available cameras',
() async {
// Arrange
// This deliberately uses 'dynamic' since that's what actual platform
// channel results will be, so using typed mock data could mask type
// handling bugs in the code under test.
final List<dynamic> returnData = <dynamic>[
<String, dynamic>{
'name': 'Test 1',
Expand All @@ -504,11 +507,13 @@ void main() {
]);
expect(cameras.length, returnData.length);
for (int i = 0; i < returnData.length; i++) {
final Map<String, Object?> typedData =
(returnData[i] as Map<dynamic, dynamic>).cast<String, Object?>();
final CameraDescription cameraDescription = CameraDescription(
name: returnData[i]['name']! as String,
name: typedData['name']! as String,
lensDirection:
parseCameraLensDirection(returnData[i]['lensFacing']! as String),
sensorOrientation: returnData[i]['sensorOrientation']! as int,
parseCameraLensDirection(typedData['lensFacing']! as String),
sensorOrientation: typedData['sensorOrientation']! as int,
);
expect(cameras[i], cameraDescription);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/camera/camera_platform_interface/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.3.3

* Updates code for stricter lint checks.

## 2.3.2

* Updates MethodChannelCamera to have startVideoRecording call the newer startVideoCapturing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,9 +551,9 @@ class MethodChannelCamera extends CameraPlatform {
Future<dynamic> handleDeviceMethodCall(MethodCall call) async {
switch (call.method) {
case 'orientation_changed':
final Map<String, Object?> arguments = _getArgumentDictionary(call);
deviceEventStreamController.add(DeviceOrientationChangedEvent(
deserializeDeviceOrientation(
call.arguments['orientation']! as String)));
deserializeDeviceOrientation(arguments['orientation']! as String)));
break;
default:
throw MissingPluginException();
Expand All @@ -568,21 +568,23 @@ class MethodChannelCamera extends CameraPlatform {
Future<dynamic> handleCameraMethodCall(MethodCall call, int cameraId) async {
switch (call.method) {
case 'initialized':
final Map<String, Object?> arguments = _getArgumentDictionary(call);
cameraEventStreamController.add(CameraInitializedEvent(
cameraId,
call.arguments['previewWidth']! as double,
call.arguments['previewHeight']! as double,
deserializeExposureMode(call.arguments['exposureMode']! as String),
call.arguments['exposurePointSupported']! as bool,
deserializeFocusMode(call.arguments['focusMode']! as String),
call.arguments['focusPointSupported']! as bool,
arguments['previewWidth']! as double,
arguments['previewHeight']! as double,
deserializeExposureMode(arguments['exposureMode']! as String),
arguments['exposurePointSupported']! as bool,
deserializeFocusMode(arguments['focusMode']! as String),
arguments['focusPointSupported']! as bool,
));
break;
case 'resolution_changed':
final Map<String, Object?> arguments = _getArgumentDictionary(call);
cameraEventStreamController.add(CameraResolutionChangedEvent(
cameraId,
call.arguments['captureWidth']! as double,
call.arguments['captureHeight']! as double,
arguments['captureWidth']! as double,
arguments['captureHeight']! as double,
));
break;
case 'camera_closing':
Expand All @@ -591,23 +593,32 @@ class MethodChannelCamera extends CameraPlatform {
));
break;
case 'video_recorded':
final Map<String, Object?> arguments = _getArgumentDictionary(call);
cameraEventStreamController.add(VideoRecordedEvent(
cameraId,
XFile(call.arguments['path']! as String),
call.arguments['maxVideoDuration'] != null
? Duration(
milliseconds: call.arguments['maxVideoDuration']! as int)
XFile(arguments['path']! as String),
arguments['maxVideoDuration'] != null
? Duration(milliseconds: arguments['maxVideoDuration']! as int)
: null,
));
break;
case 'error':
final Map<String, Object?> arguments = _getArgumentDictionary(call);
cameraEventStreamController.add(CameraErrorEvent(
cameraId,
call.arguments['description']! as String,
arguments['description']! as String,
));
break;
default:
throw MissingPluginException();
}
}

/// Returns the arguments of [call] as typed string-keyed Map.
///
/// This does not do any type validation, so is only safe to call if the
/// arguments are known to be a map.
Map<String, Object?> _getArgumentDictionary(MethodCall call) {
return (call.arguments as Map<Object?, Object?>).cast<String, Object?>();
}
}
2 changes: 1 addition & 1 deletion packages/camera/camera_platform_interface/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ repository: https://github.com/flutter/plugins/tree/main/packages/camera/camera_
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 2.3.2
version: 2.3.3

environment:
sdk: '>=2.12.0 <3.0.0'
Expand Down
Loading

0 comments on commit 9fdc899

Please sign in to comment.