Skip to content

Commit 1dc73a1

Browse files
[share_plus] migrate to null safety (#116)
* migrated platform interface * migrated windows * migrated web * migrated linux * migrated share_plus * fix tests * flutter format * renamed web integration tests to avoid issues with package validation
1 parent b086883 commit 1dc73a1

22 files changed

+101
-100
lines changed

packages/share_plus/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.0.0-nullsafety.0
2+
3+
- Migrated to null safety
4+
15
## 1.2.0
26

37
- Add Web support (`share_plus_web`)

packages/share_plus/lib/share_plus.dart

+9-11
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Share {
2222
}
2323

2424
static bool _disablePlatformOverride = false;
25-
static SharePlatform __platform;
25+
static SharePlatform? __platform;
2626

2727
// This is to manually endorse the Linux plugin until automatic registration
2828
// of dart plugins is implemented.
@@ -38,7 +38,7 @@ class Share {
3838
}
3939
__platform ??= SharePlatform.instance;
4040
}
41-
return __platform;
41+
return __platform!;
4242
}
4343

4444
/// Summons the platform's share sheet to share text.
@@ -58,10 +58,9 @@ class Share {
5858
/// from [MethodChannel].
5959
static Future<void> share(
6060
String text, {
61-
String subject,
62-
Rect sharePositionOrigin,
61+
String? subject,
62+
Rect? sharePositionOrigin,
6363
}) {
64-
assert(text != null);
6564
assert(text.isNotEmpty);
6665
return _platform.share(
6766
text,
@@ -84,14 +83,13 @@ class Share {
8483
/// from [MethodChannel].
8584
static Future<void> shareFiles(
8685
List<String> paths, {
87-
List<String> mimeTypes,
88-
String subject,
89-
String text,
90-
Rect sharePositionOrigin,
86+
List<String>? mimeTypes,
87+
String? subject,
88+
String? text,
89+
Rect? sharePositionOrigin,
9190
}) {
92-
assert(paths != null);
9391
assert(paths.isNotEmpty);
94-
assert(paths.every((element) => element != null && element.isNotEmpty));
92+
assert(paths.every((element) => element.isNotEmpty));
9593
return _platform.shareFiles(
9694
paths,
9795
mimeTypes: mimeTypes,

packages/share_plus/pubspec.yaml

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: share_plus
22
description: Flutter plugin for sharing content via the platform share UI, using the ACTION_SEND intent on Android and UIActivityViewController on iOS.
3-
version: 1.2.0
3+
version: 2.0.0-nullsafety.0
44
homepage: https://plus.fluttercommunity.dev/
55
repository: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/
66

@@ -21,22 +21,19 @@ flutter:
2121

2222
dependencies:
2323
meta: ^1.0.5
24-
mime: ^0.9.7
24+
mime: ^1.0.0-nullsafety
2525
flutter:
2626
sdk: flutter
27-
share_plus_platform_interface: ^1.2.0
28-
share_plus_linux: ^1.1.0
29-
share_plus_windows: ^0.1.0
30-
share_plus_web: ^0.1.0
27+
share_plus_platform_interface: ^2.0.0-nullsafety
28+
share_plus_linux: ^2.0.0-nullsafety
29+
share_plus_windows: ^0.2.0-nullsafety
30+
share_plus_web: ^0.2.0-nullsafety
3131

3232
dev_dependencies:
33-
test: ^1.3.0
34-
mockito: ^3.0.0
3533
flutter_test:
3634
sdk: flutter
37-
url_launcher: ^5.7.2
3835
pedantic: ^1.9.2
3936

4037
environment:
41-
sdk: ">=2.1.0 <3.0.0"
38+
sdk: ">=2.12.0-0 <3.0.0"
4239
flutter: ">=1.12.13+hotfix.5 <2.0.0"

packages/share_plus_linux/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.0.0-nullsafety.0
2+
3+
- Migrated to null safety
4+
15
## 1.1.0
26

37
- Transfer to plus-plugins monorepo

packages/share_plus_linux/lib/share_plus_linux.dart

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class ShareLinux extends SharePlatform {
1212
@override
1313
Future<void> share(
1414
String text, {
15-
String subject,
16-
Rect sharePositionOrigin,
15+
String? subject,
16+
Rect? sharePositionOrigin,
1717
}) {
1818
final uri = Uri.encodeFull('mailto:?subject=$subject&body=$text');
1919
return launch(uri);
@@ -23,10 +23,10 @@ class ShareLinux extends SharePlatform {
2323
@override
2424
Future<void> shareFiles(
2525
List<String> paths, {
26-
List<String> mimeTypes,
27-
String subject,
28-
String text,
29-
Rect sharePositionOrigin,
26+
List<String>? mimeTypes,
27+
String? subject,
28+
String? text,
29+
Rect? sharePositionOrigin,
3030
}) {
3131
throw UnimplementedError('shareFiles() has not been implemented on Linux.');
3232
}

packages/share_plus_linux/pubspec.yaml

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
name: share_plus_linux
22
description: Linux implementation of the share_plus plugin
3-
version: 1.1.0
3+
version: 2.0.0-nullsafety.0
44
homepage: https://plus.fluttercommunity.dev/
55
repository: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/
66

77
environment:
8-
sdk: ">=2.7.0 <3.0.0"
8+
sdk: ">=2.12.0-0 <3.0.0"
99
flutter: ">=1.17.0 <2.0.0"
1010

1111
dependencies:
12-
share_plus_platform_interface: ^1.1.0
13-
file: '>=5.2.1 <=6.0.0'
12+
share_plus_platform_interface: ^2.0.0-nullsafety
13+
file: ^6.0.0-nullsafety
1414
flutter:
1515
sdk: flutter
1616
meta: ^1.2.3
17-
url_launcher: ^5.7.2
17+
url_launcher: ^6.0.0-nullsafety
1818

1919
dev_dependencies:
2020
flutter_test:

packages/share_plus_platform_interface/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.0.0-nullsafety.0
2+
3+
- Migrated to null safety
4+
15
## 1.2.0
26

37
- Rename method channel to avoid conflicts

packages/share_plus_platform_interface/lib/method_channel/method_channel_share.dart

+7-10
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ class MethodChannelShare extends SharePlatform {
2222
@override
2323
Future<void> share(
2424
String text, {
25-
String subject,
26-
Rect sharePositionOrigin,
25+
String? subject,
26+
Rect? sharePositionOrigin,
2727
}) {
28-
assert(text != null);
2928
assert(text.isNotEmpty);
3029
final params = <String, dynamic>{
3130
'text': text,
@@ -46,14 +45,13 @@ class MethodChannelShare extends SharePlatform {
4645
@override
4746
Future<void> shareFiles(
4847
List<String> paths, {
49-
List<String> mimeTypes,
50-
String subject,
51-
String text,
52-
Rect sharePositionOrigin,
48+
List<String>? mimeTypes,
49+
String? subject,
50+
String? text,
51+
Rect? sharePositionOrigin,
5352
}) {
54-
assert(paths != null);
5553
assert(paths.isNotEmpty);
56-
assert(paths.every((element) => element != null && element.isNotEmpty));
54+
assert(paths.every((element) => element.isNotEmpty));
5755
final params = <String, dynamic>{
5856
'paths': paths,
5957
'mimeTypes': mimeTypes ??
@@ -74,7 +72,6 @@ class MethodChannelShare extends SharePlatform {
7472
}
7573

7674
static String _mimeTypeForPath(String path) {
77-
assert(path != null);
7875
return lookupMimeType(path) ?? 'application/octet-stream';
7976
}
8077
}

packages/share_plus_platform_interface/lib/share_plus_platform_interface.dart

+6-6
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ class SharePlatform extends PlatformInterface {
3333
/// Share text.
3434
Future<void> share(
3535
String text, {
36-
String subject,
37-
Rect sharePositionOrigin,
36+
String? subject,
37+
Rect? sharePositionOrigin,
3838
}) {
3939
return _instance.share(
4040
text,
@@ -46,10 +46,10 @@ class SharePlatform extends PlatformInterface {
4646
/// Share files.
4747
Future<void> shareFiles(
4848
List<String> paths, {
49-
List<String> mimeTypes,
50-
String subject,
51-
String text,
52-
Rect sharePositionOrigin,
49+
List<String>? mimeTypes,
50+
String? subject,
51+
String? text,
52+
Rect? sharePositionOrigin,
5353
}) {
5454
return _instance.shareFiles(
5555
paths,
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
name: share_plus_platform_interface
22
description: A common platform interface for the share_plus plugin.
3-
version: 1.2.0
3+
version: 2.0.0-nullsafety.0
44
homepage: https://plus.fluttercommunity.dev/
55
repository: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/
66

77
dependencies:
88
flutter:
99
sdk: flutter
1010
meta: ^1.1.8
11-
mime: ^0.9.7
12-
plugin_platform_interface: ^1.0.2
11+
mime: ^1.0.0-nullsafety
12+
plugin_platform_interface: ^1.1.0-nullsafety
1313

1414
dev_dependencies:
1515
flutter_test:
1616
sdk: flutter
17-
mockito: ^3.0.0
17+
mockito: ^5.0.0-nullsafety
1818
pedantic: ^1.9.2
1919
test: ^1.3.0
2020

2121
environment:
22-
sdk: ">=2.1.0 <3.0.0"
22+
sdk: ">=2.12.0-0 <3.0.0"
2323
flutter: ">=1.12.13+hotfix.5 <2.0.0"

packages/share_plus_platform_interface/test/share_plus_platform_interface_test.dart

+10-18
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import 'package:flutter/services.dart';
1616
void main() {
1717
TestWidgetsFlutterBinding.ensureInitialized();
1818

19-
MockMethodChannel mockChannel;
19+
late MockMethodChannel mockChannel;
2020

2121
setUp(() {
2222
mockChannel = MockMethodChannel();
@@ -28,14 +28,6 @@ void main() {
2828
});
2929
});
3030

31-
test('sharing null fails', () {
32-
expect(
33-
() => SharePlatform.instance.share(null),
34-
throwsA(const TypeMatcher<AssertionError>()),
35-
);
36-
verifyZeroInteractions(mockChannel);
37-
});
38-
3931
test('sharing empty fails', () {
4032
expect(
4133
() => SharePlatform.instance.share(''),
@@ -60,14 +52,6 @@ void main() {
6052
}));
6153
});
6254

63-
test('sharing null file fails', () {
64-
expect(
65-
() => SharePlatform.instance.shareFiles([null]),
66-
throwsA(const TypeMatcher<AssertionError>()),
67-
);
68-
verifyZeroInteractions(mockChannel);
69-
});
70-
7155
test('sharing empty file fails', () {
7256
expect(
7357
() => SharePlatform.instance.shareFiles(['']),
@@ -107,4 +91,12 @@ void main() {
10791
});
10892
}
10993

110-
class MockMethodChannel extends Mock implements MethodChannel {}
94+
// https://github.com/dart-lang/mockito/issues/316
95+
class MockMethodChannel extends Mock implements MethodChannel {
96+
@override
97+
Future<T?> invokeMethod<T>(String method, [dynamic arguments]) async {
98+
return super
99+
.noSuchMethod(Invocation.method(#invokeMethod, [method, arguments]))
100+
as dynamic;
101+
}
102+
}

packages/share_plus_web/CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
# 0.1.0
1+
## 0.2.0-nullsafety.0
2+
3+
- Migrated to null safety
4+
5+
## 0.1.0
26

37
- Initial open-source release.

packages/share_plus_web/lib/share_plus_web.dart

+11-9
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,36 @@ class SharePlusPlugin extends SharePlatform {
1616
final _navigator;
1717

1818
/// A constructor that allows tests to override the window object used by the plugin.
19-
SharePlusPlugin({@visibleForTesting html.Navigator debugNavigator})
19+
SharePlusPlugin({@visibleForTesting html.Navigator? debugNavigator})
2020
: _navigator = debugNavigator ?? html.window.navigator;
2121

2222
@override
2323

2424
/// Share text
2525
Future<void> share(
2626
String text, {
27-
String subject,
28-
Rect sharePositionOrigin,
27+
String? subject,
28+
Rect? sharePositionOrigin,
2929
}) async {
3030
try {
3131
await _navigator.share({'title': subject, 'text': text});
3232
} catch (e) {
3333
//Navigator is not available or the webPage is not served on https
3434
final uri = Uri.encodeFull('mailto:?subject=$subject&body=$text');
35-
return launch(uri);
35+
await launch(uri);
3636
}
3737
}
3838

3939
@override
4040

4141
/// Share files
42-
Future<void> shareFiles(List<String> paths,
43-
{List<String> mimeTypes,
44-
String subject,
45-
String text,
46-
Rect sharePositionOrigin}) {
42+
Future<void> shareFiles(
43+
List<String> paths, {
44+
List<String>? mimeTypes,
45+
String? subject,
46+
String? text,
47+
Rect? sharePositionOrigin,
48+
}) {
4749
throw UnimplementedError('shareFiles() has not been implemented on Web.');
4850
}
4951
}

0 commit comments

Comments
 (0)