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

[android_intent] Migrate to nnbd #3328

Merged
merged 2 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/android_intent/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.0-nullsafety

* Migrate to null safety.

## 0.3.7+8

* Update Flutter SDK constraint.
Expand Down
27 changes: 14 additions & 13 deletions packages/android_intent/lib/android_intent.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class AndroidIntent {
this.arguments,
this.package,
this.componentName,
Platform platform,
Platform? platform,
this.type,
}) : assert(action != null || componentName != null,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you remind me why assert is still needed in g3?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This particular assert is useful because it checks that either action or componentName is non-null. Therefore, both are nullable.

For non-nullable vars, asserts are useful for mixed mode. If an app that is not yet migrated uses a null-safe library, they can still pass null to a non-nullable type. This will eventually fail with a _CastError if we remove the asserts. The assert is more explicit and meaningful. When most code in Dart ecosystem is migrated to null-safety and/or we have had a couple of stable releases of nnbd, we can start cleaning up the asserts. package:flutter is following the same thing.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense! thanks

'action or component (or both) must be specified'),
Expand All @@ -47,8 +47,8 @@ class AndroidIntent {
/// app code, it may break without warning.
@visibleForTesting
AndroidIntent.private({
@required Platform platform,
@required MethodChannel channel,
required Platform platform,
required MethodChannel channel,
this.action,
this.flags,
this.category,
Expand All @@ -66,47 +66,47 @@ class AndroidIntent {
/// includes constants like `ACTION_VIEW`.
///
/// See https://developer.android.com/reference/android/content/Intent.html#intent-structure.
final String action;
final String? action;

/// Constants that can be set on an intent to tweak how it is finally handled.
/// Some of the constants are mirrored to Dart via [Flag].
///
/// See https://developer.android.com/reference/android/content/Intent.html#setFlags(int).
final List<int> flags;
final List<int>? flags;

/// An optional additional constant qualifying the given [action].
///
/// See https://developer.android.com/reference/android/content/Intent.html#intent-structure.
final String category;
final String? category;

/// The Uri that the [action] is pointed towards.
///
/// See https://developer.android.com/reference/android/content/Intent.html#intent-structure.
final String data;
final String? data;

/// The equivalent of `extras`, a generic `Bundle` of data that the Intent can
/// carry. This is a slot for extraneous data that the listener may use.
///
/// See https://developer.android.com/reference/android/content/Intent.html#intent-structure.
final Map<String, dynamic> arguments;
final Map<String, dynamic>? arguments;

/// Sets the [data] to only resolve within this given package.
///
/// See https://developer.android.com/reference/android/content/Intent.html#setPackage(java.lang.String).
final String package;
final String? package;

/// Set the exact `ComponentName` that should handle the intent. If this is
/// set [package] should also be non-null.
///
/// See https://developer.android.com/reference/android/content/Intent.html#setComponent(android.content.ComponentName).
final String componentName;
final String? componentName;
final MethodChannel _channel;
final Platform _platform;

/// Set an explicit MIME data type.
///
/// See https://developer.android.com/reference/android/content/Intent.html#intent-structure.
final String type;
final String? type;

bool _isPowerOfTwo(int x) {
/* First x in the below expression is for the case when x is 0 */
Expand Down Expand Up @@ -146,17 +146,18 @@ class AndroidIntent {
return false;
}

return await _channel.invokeMethod<bool>(
final result = await _channel.invokeMethod<bool>(
'canResolveActivity',
_buildArguments(),
);
return result!;
}

/// Constructs the map of arguments which is passed to the plugin.
Map<String, dynamic> _buildArguments() {
return {
if (action != null) 'action': action,
if (flags != null) 'flags': convertFlags(flags),
if (flags != null) 'flags': convertFlags(flags!),
if (category != null) 'category': category,
if (data != null) 'data': data,
if (arguments != null) 'arguments': arguments,
Expand Down
17 changes: 7 additions & 10 deletions packages/android_intent/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
name: android_intent
description: Flutter plugin for launching Android Intents. Not supported on iOS.
homepage: https://github.com/flutter/plugins/tree/master/packages/android_intent
# 0.3.y+z is compatible with 1.0.0, if you land a breaking change bump
# the version to 2.0.0.
# See more details: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0
version: 0.3.7+8
version: 2.0.0-nullsafety

flutter:
plugin:
Expand All @@ -16,15 +13,15 @@ flutter:
dependencies:
flutter:
sdk: flutter
platform: ">=2.0.0 <4.0.0"
meta: ^1.0.5
platform: ^3.0.0-nullsafety.4
meta: ^1.3.0-nullsafety.6
dev_dependencies:
test: ^1.3.0
mockito: ^3.0.0
test: ^1.16.0-nullsafety.13
mockito: ^4.1.3
flutter_test:
sdk: flutter
pedantic: ^1.8.0
pedantic: ^1.10.0-nullsafety.1

environment:
sdk: ">=2.3.0 <3.0.0"
sdk: ">=2.12.0-0 <3.0.0"
flutter: ">=1.12.13+hotfix.5"
4 changes: 3 additions & 1 deletion packages/android_intent/test/android_intent_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import 'package:platform/platform.dart';

void main() {
AndroidIntent androidIntent;
MockMethodChannel mockChannel;
late MockMethodChannel mockChannel;
setUp(() {
mockChannel = MockMethodChannel();
when(mockChannel.invokeMethod<bool>('canResolveActivity', any))
.thenAnswer((realInvocation) async => true);
});

group('AndroidIntent', () {
Expand Down
1 change: 1 addition & 0 deletions script/nnbd_plugins.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# null-safe is available on stable.

readonly NNBD_PLUGINS_LIST=(
"android_intent"
"connectivity"
"device_info"
"flutter_plugin_android_lifecycle"
Expand Down