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

[connectivity][connectvity_platform_interface] migrate to nnbd #3176

Merged
merged 13 commits into from
Oct 23, 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/connectivity/connectivity/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.0.0-nullsafety

* Migrate to null safety.

## 2.0.0

* [Breaking Change] The `getWifiName`, `getWifiBSSID` and `getWifiIP` are removed to [wifi_info_flutter](https://github.com/flutter/plugins/tree/master/packages/wifi_info_flutter)
Expand Down
4 changes: 4 additions & 0 deletions packages/connectivity/connectivity/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include: ../../../analysis_options.yaml
analyzer:
enable-experiment:
- non-nullable
6 changes: 3 additions & 3 deletions packages/connectivity/connectivity/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class MyApp extends StatelessWidget {
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
MyHomePage({Key? key, required this.title}) : super(key: key);

final String title;

Expand All @@ -51,7 +51,7 @@ class MyHomePage extends StatefulWidget {
class _MyHomePageState extends State<MyHomePage> {
String _connectionStatus = 'Unknown';
final Connectivity _connectivity = Connectivity();
StreamSubscription<ConnectivityResult> _connectivitySubscription;
late StreamSubscription<ConnectivityResult> _connectivitySubscription;

@override
void initState() {
Expand All @@ -69,7 +69,7 @@ class _MyHomePageState extends State<MyHomePage> {

// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initConnectivity() async {
ConnectivityResult result;
ConnectivityResult result = ConnectivityResult.none;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
result = await _connectivity.checkConnectivity();
Expand Down
4 changes: 2 additions & 2 deletions packages/connectivity/connectivity/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ dependencies:
dev_dependencies:
flutter_driver:
sdk: flutter
test: any
test: ^1.10.0-nullsafety.1
integration_test:
path: ../../../integration_test
pedantic: ^1.8.0
pedantic: ^1.10.0-nullsafety.1

flutter:
uses-material-design: true
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// TODO(cyanglaz): Remove once https://github.com/flutter/plugins/pull/3158 is landed.
// @dart = 2.9

import 'package:integration_test/integration_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:connectivity/connectivity.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// TODO(cyanglaz): Remove once https://github.com/flutter/plugins/pull/3158 is landed.
// @dart = 2.9

import 'package:integration_test/integration_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:connectivity/connectivity.dart';


void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

Expand Down
4 changes: 2 additions & 2 deletions packages/connectivity/connectivity/lib/connectivity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ class Connectivity {
if (_singleton == null) {
_singleton = Connectivity._();
}
return _singleton;
return _singleton!;
Copy link

Choose a reason for hiding this comment

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

Interesting. In some cases the analyzer is smart enough to figure out that _singleton cannot be null at this point, so ! isn't required.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got an error trying to remove !

./lib/connectivity.dart:25:12: Error: A value of type 'Connectivity?' can't be returned from a function with return type 'Connectivity'.
 - 'Connectivity' is from 'package:connectivity/connectivity.dart' ('../lib/connectivity.dart').
    return _singleton;```

}

Connectivity._();

static Connectivity _singleton;
static Connectivity? _singleton;

static ConnectivityPlatform get _platform => ConnectivityPlatform.instance;

Expand Down
21 changes: 14 additions & 7 deletions packages/connectivity/connectivity/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ name: connectivity
description: Flutter plugin for discovering the state of the network (WiFi &
mobile/cellular) connectivity on Android and iOS.
homepage: https://github.com/flutter/plugins/tree/master/packages/connectivity/connectivity
version: 2.0.0
version: 3.0.0-nullsafety

# Don't publish this package until null safety is stable or the package is in the allow list.
publish_to: none

flutter:
plugin:
Expand All @@ -21,22 +24,26 @@ dependencies:
flutter:
sdk: flutter
meta: ^1.0.5
connectivity_platform_interface: ^1.0.2
connectivity_platform_interface:
path: ../connectivity_platform_interface
#TODO(cyanglaz): re-endorse the below plugins when they have migrated to nnbd.
# https://github.com/flutter/flutter/issues/68669
connectivity_macos: ^0.1.0
connectivity_for_web: ^0.3.0
# connectivity_for_web: ^0.3.0

dev_dependencies:
flutter_test:
sdk: flutter
flutter_driver:
sdk: flutter
test: any
test: ^1.10.0-nullsafety.1
integration_test:
path: ../../integration_test
mockito: ^4.1.1
plugin_platform_interface: ^1.0.0
pedantic: ^1.8.0
plugin_platform_interface:
path: ../../plugin_platform_interface
pedantic: ^1.10.0-nullsafety.1

environment:
sdk: ">=2.1.0 <3.0.0"
sdk: '>=2.10.0-56.0.dev <3.0.0'
flutter: ">=1.12.13+hotfix.5 <2.0.0"
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// TODO(cyanglaz): Remove once Mockito is migrated to null safety.
// @dart = 2.9

import 'package:connectivity/connectivity.dart';
import 'package:connectivity_platform_interface/connectivity_platform_interface.dart';
import 'package:flutter_test/flutter_test.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.0-nullsafety

* Migrate to null safety.

## 1.0.6

* Update lower bound of dart dependency to 2.1.0.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include: ../../../analysis_options.yaml
analyzer:
enable-experiment:
- non-nullable
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ abstract class ConnectivityPlatform extends PlatformInterface {
}

/// Obtains the wifi name (SSID) of the connected network
Future<String> getWifiName() {
Future<String?> getWifiName() {
throw UnimplementedError('getWifiName() has not been implemented.');
}

/// Obtains the wifi BSSID of the connected network.
Future<String> getWifiBSSID() {
Future<String?> getWifiBSSID() {
throw UnimplementedError('getWifiBSSID() has not been implemented.');
}

/// Obtains the IP address of the connected wifi network
Future<String> getWifiIP() {
Future<String?> getWifiIP() {
throw UnimplementedError('getWifiIP() has not been implemented.');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:connectivity_platform_interface/connectivity_platform_interface.
import 'package:flutter/services.dart';
import 'package:meta/meta.dart';

import 'utils.dart';
import 'utils.dart';

/// An implementation of [ConnectivityPlatform] that uses method channels.
Expand All @@ -22,29 +23,29 @@ class MethodChannelConnectivity extends ConnectivityPlatform {
EventChannel eventChannel =
EventChannel('plugins.flutter.io/connectivity_status');

Stream<ConnectivityResult> _onConnectivityChanged;
Stream<ConnectivityResult>? _onConnectivityChanged;

/// Fires whenever the connectivity state changes.
Stream<ConnectivityResult> get onConnectivityChanged {
if (_onConnectivityChanged == null) {
_onConnectivityChanged = eventChannel
.receiveBroadcastStream()
.map((dynamic result) => result.toString())
.map(parseConnectivityResult);
_onConnectivityChanged =
eventChannel.receiveBroadcastStream().map((dynamic result) {
return result != null ? result.toString() : '';
}).map(parseConnectivityResult);
}
return _onConnectivityChanged;
return _onConnectivityChanged!;
}

@override
Future<ConnectivityResult> checkConnectivity() {
return methodChannel
.invokeMethod<String>('check')
.then(parseConnectivityResult);
Future<ConnectivityResult> checkConnectivity() async {
final String checkResult =
await methodChannel.invokeMethod<String>('check') ?? '';
return parseConnectivityResult(checkResult);
}

@override
Future<String> getWifiName() async {
String wifiName = await methodChannel.invokeMethod<String>('wifiName');
Future<String?> getWifiName() async {
String? wifiName = await methodChannel.invokeMethod<String>('wifiName');
// as Android might return <unknown ssid>, uniforming result
// our iOS implementation will return null
if (wifiName == '<unknown ssid>') {
Expand All @@ -54,29 +55,31 @@ class MethodChannelConnectivity extends ConnectivityPlatform {
}

@override
Future<String> getWifiBSSID() {
Future<String?> getWifiBSSID() {
return methodChannel.invokeMethod<String>('wifiBSSID');
}

@override
Future<String> getWifiIP() {
Future<String?> getWifiIP() {
return methodChannel.invokeMethod<String>('wifiIPAddress');
}

@override
Future<LocationAuthorizationStatus> requestLocationServiceAuthorization({
bool requestAlwaysLocationUsage = false,
}) {
return methodChannel.invokeMethod<String>(
'requestLocationServiceAuthorization', <bool>[
requestAlwaysLocationUsage
]).then(parseLocationAuthorizationStatus);
}) async {
final String requestLocationServiceResult = await methodChannel
.invokeMethod<String>('requestLocationServiceAuthorization',
<bool>[requestAlwaysLocationUsage]) ??
'';
return parseLocationAuthorizationStatus(requestLocationServiceResult);
}

@override
Future<LocationAuthorizationStatus> getLocationServiceAuthorization() {
return methodChannel
.invokeMethod<String>('getLocationServiceAuthorization')
.then(parseLocationAuthorizationStatus);
Future<LocationAuthorizationStatus> getLocationServiceAuthorization() async {
final String getLocationServiceResult = await methodChannel
.invokeMethod<String>('getLocationServiceAuthorization') ??
'';
return parseLocationAuthorizationStatus(getLocationServiceResult);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@ description: A common platform interface for the connectivity plugin.
homepage: https://github.com/flutter/plugins/tree/master/packages/connectivity/connectivity_platform_interface
# 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: 1.0.6
version: 2.0.0-nullsafety

# Don't publish this package until null safety is stable or the package is in the allow list.
publish_to: none

dependencies:
flutter:
sdk: flutter
meta: ^1.0.5
plugin_platform_interface: ^1.0.1
meta: ^1.3.0-nullsafety.3
plugin_platform_interface:
path: ../../plugin_platform_interface

dev_dependencies:
flutter_test:
sdk: flutter
pedantic: ^1.8.0
pedantic: ^1.10.0-nullsafety.1

environment:
sdk: ">=2.1.0 <3.0.0"
sdk: '>=2.10.0-56.0.dev <3.0.0'
flutter: ">=1.12.13+hotfix.5 <2.0.0"
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void main() {

group('$MethodChannelConnectivity', () {
final List<MethodCall> log = <MethodCall>[];
MethodChannelConnectivity methodChannelConnectivity;
late MethodChannelConnectivity methodChannelConnectivity;

setUp(() async {
methodChannelConnectivity = MethodChannelConnectivity();
Expand Down Expand Up @@ -42,7 +42,7 @@ void main() {
.setMockMethodCallHandler((MethodCall methodCall) async {
switch (methodCall.method) {
case 'listen':
await ServicesBinding.instance.defaultBinaryMessenger
await ServicesBinding.instance!.defaultBinaryMessenger
.handlePlatformMessage(
methodChannelConnectivity.eventChannel.name,
methodChannelConnectivity.eventChannel.codec
Expand All @@ -64,7 +64,7 @@ void main() {
});

test('getWifiName', () async {
final String result = await methodChannelConnectivity.getWifiName();
final String? result = await methodChannelConnectivity.getWifiName();
expect(result, '1337wifi');
expect(
log,
Expand All @@ -78,7 +78,7 @@ void main() {
});

test('getWifiBSSID', () async {
final String result = await methodChannelConnectivity.getWifiBSSID();
final String? result = await methodChannelConnectivity.getWifiBSSID();
expect(result, 'c0:ff:33:c0:d3:55');
expect(
log,
Expand All @@ -92,7 +92,7 @@ void main() {
});

test('getWifiIP', () async {
final String result = await methodChannelConnectivity.getWifiIP();
final String? result = await methodChannelConnectivity.getWifiIP();
expect(result, '127.0.0.1');
expect(
log,
Expand Down
2 changes: 2 additions & 0 deletions script/incremental_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ CUSTOM_ANALYSIS_PLUGINS=(
"url_launcher/url_launcher"
"device_info/device_info_platform_interface"
"device_info/device_info"
"connectivity/connectivity_platform_interface"
"connectivity/connectivity"
"url_launcher/url_launcher_web"
)
# Comma-separated string of the list above
Expand Down