Skip to content

Commit

Permalink
Migrate plugin to null safety. (#791)
Browse files Browse the repository at this point in the history
* Update dependencies. Generate new protobuf files.

* Allow Timeout parameter for scans to be null. Fix private constructor.

* Remove const constructor with named parameters for ScanResult and AdvertisementData.

These classes should only ever be created using the .fromProto method, since they link directly to objects that exist platform-side.
If the end-user needs to create their own ScanResult/AdvertisementData, they should use their own models within their app.

* Secondary GUID can be null.
lastValue for BluetoothCharacteristic and Descriptor will return an empty data list by default. Can never return null.

* Revert to Protobuf 3.11.4.

* Quick and dirty conversion of example app to Null safety. Needs cleanup.

* Generate protobuf files with new null safe protoc_plugin.

* Remove unused E2E from Android embedder.

* Allow TxPowerLevel to be null.

* Null check not necessary in toString functions.

* Force non-null for writeType when receiving from platform.

Consider performing a more explicit check of the proto value, and make sure it maps correctly to the Dart CharacteristicWriteType value.

* Remove unused E2E test file.

E2E has moved to a new package, integration_test, and will need to be re-implemented.

* Add 0.8.0 notes to CHANGELOG
  • Loading branch information
pauldemarco committed Mar 14, 2021
1 parent c19bb9a commit 8d8d954
Show file tree
Hide file tree
Showing 15 changed files with 1,419 additions and 1,472 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.8.0
* Migrate the plugin to null safety.

## 0.7.3
* Fix Android project template files to be compatible with protobuf-lite.
* Add experimental support for MacOS.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.pauldemarco.flutter_blue_example;

import android.os.Bundle;
import dev.flutter.plugins.e2e.E2EPlugin;
import io.flutter.app.FlutterActivity;
import com.pauldemarco.flutter_blue.FlutterBluePlugin;

Expand All @@ -10,6 +9,5 @@ public class EmbeddingV1Activity extends FlutterActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FlutterBluePlugin.registerWith(registrarFor("com.pauldemarco.flutter_blue.FlutterBluePlugin"));
E2EPlugin.registerWith(registrarFor("dev.flutter.plugins.e2e.E2EPlugin"));
}
}
22 changes: 11 additions & 11 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ class FlutterBlueApp extends StatelessWidget {
}

class BluetoothOffScreen extends StatelessWidget {
const BluetoothOffScreen({Key key, this.state}) : super(key: key);
const BluetoothOffScreen({Key? key, this.state}) : super(key: key);

final BluetoothState state;
final BluetoothState? state;

@override
Widget build(BuildContext context) {
Expand All @@ -55,7 +55,7 @@ class BluetoothOffScreen extends StatelessWidget {
style: Theme.of(context)
.primaryTextTheme
.subhead
.copyWith(color: Colors.white),
?.copyWith(color: Colors.white),
),
],
),
Expand All @@ -82,7 +82,7 @@ class FindDevicesScreen extends StatelessWidget {
.asyncMap((_) => FlutterBlue.instance.connectedDevices),
initialData: [],
builder: (c, snapshot) => Column(
children: snapshot.data
children: snapshot.data!
.map((d) => ListTile(
title: Text(d.name),
subtitle: Text(d.id.toString()),
Expand Down Expand Up @@ -111,7 +111,7 @@ class FindDevicesScreen extends StatelessWidget {
stream: FlutterBlue.instance.scanResults,
initialData: [],
builder: (c, snapshot) => Column(
children: snapshot.data
children: snapshot.data!
.map(
(r) => ScanResultTile(
result: r,
Expand All @@ -133,7 +133,7 @@ class FindDevicesScreen extends StatelessWidget {
stream: FlutterBlue.instance.isScanning,
initialData: false,
builder: (c, snapshot) {
if (snapshot.data) {
if (snapshot.data!) {
return FloatingActionButton(
child: Icon(Icons.stop),
onPressed: () => FlutterBlue.instance.stopScan(),
Expand All @@ -152,7 +152,7 @@ class FindDevicesScreen extends StatelessWidget {
}

class DeviceScreen extends StatelessWidget {
const DeviceScreen({Key key, this.device}) : super(key: key);
const DeviceScreen({Key? key, required this.device}) : super(key: key);

final BluetoothDevice device;

Expand Down Expand Up @@ -211,7 +211,7 @@ class DeviceScreen extends StatelessWidget {
stream: device.state,
initialData: BluetoothDeviceState.connecting,
builder: (c, snapshot) {
VoidCallback onPressed;
VoidCallback? onPressed;
String text;
switch (snapshot.data) {
case BluetoothDeviceState.connected:
Expand All @@ -234,7 +234,7 @@ class DeviceScreen extends StatelessWidget {
style: Theme.of(context)
.primaryTextTheme
.button
.copyWith(color: Colors.white),
?.copyWith(color: Colors.white),
));
},
)
Expand All @@ -257,7 +257,7 @@ class DeviceScreen extends StatelessWidget {
stream: device.isDiscoveringServices,
initialData: false,
builder: (c, snapshot) => IndexedStack(
index: snapshot.data ? 1 : 0,
index: snapshot.data! ? 1 : 0,
children: <Widget>[
IconButton(
icon: Icon(Icons.refresh),
Expand Down Expand Up @@ -295,7 +295,7 @@ class DeviceScreen extends StatelessWidget {
initialData: [],
builder: (c, snapshot) {
return Column(
children: _buildServiceTiles(snapshot.data),
children: _buildServiceTiles(snapshot.data!),
);
},
),
Expand Down
71 changes: 35 additions & 36 deletions example/lib/widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import 'package:flutter/material.dart';
import 'package:flutter_blue/flutter_blue.dart';

class ScanResultTile extends StatelessWidget {
const ScanResultTile({Key key, this.result, this.onTap}) : super(key: key);
const ScanResultTile({Key? key, required this.result, this.onTap})
: super(key: key);

final ScanResult result;
final VoidCallback onTap;
final VoidCallback? onTap;

Widget _buildTitle(BuildContext context) {
if (result.device.name.length > 0) {
Expand Down Expand Up @@ -48,7 +49,7 @@ class ScanResultTile extends StatelessWidget {
style: Theme.of(context)
.textTheme
.caption
.apply(color: Colors.black),
?.apply(color: Colors.black),
softWrap: true,
),
),
Expand All @@ -64,7 +65,7 @@ class ScanResultTile extends StatelessWidget {

String getNiceManufacturerData(Map<int, List<int>> data) {
if (data.isEmpty) {
return null;
return 'N/A';
}
List<String> res = [];
data.forEach((id, bytes) {
Expand All @@ -76,7 +77,7 @@ class ScanResultTile extends StatelessWidget {

String getNiceServiceData(Map<String, List<int>> data) {
if (data.isEmpty) {
return null;
return 'N/A';
}
List<String> res = [];
data.forEach((id, bytes) {
Expand All @@ -101,20 +102,16 @@ class ScanResultTile extends StatelessWidget {
context, 'Complete Local Name', result.advertisementData.localName),
_buildAdvRow(context, 'Tx Power Level',
'${result.advertisementData.txPowerLevel ?? 'N/A'}'),
_buildAdvRow(
context,
'Manufacturer Data',
getNiceManufacturerData(
result.advertisementData.manufacturerData) ??
'N/A'),
_buildAdvRow(context, 'Manufacturer Data',
getNiceManufacturerData(result.advertisementData.manufacturerData)),
_buildAdvRow(
context,
'Service UUIDs',
(result.advertisementData.serviceUuids.isNotEmpty)
? result.advertisementData.serviceUuids.join(', ').toUpperCase()
: 'N/A'),
_buildAdvRow(context, 'Service Data',
getNiceServiceData(result.advertisementData.serviceData) ?? 'N/A'),
getNiceServiceData(result.advertisementData.serviceData)),
],
);
}
Expand All @@ -124,7 +121,8 @@ class ServiceTile extends StatelessWidget {
final BluetoothService service;
final List<CharacteristicTile> characteristicTiles;

const ServiceTile({Key key, this.service, this.characteristicTiles})
const ServiceTile(
{Key? key, required this.service, required this.characteristicTiles})
: super(key: key);

@override
Expand All @@ -137,10 +135,8 @@ class ServiceTile extends StatelessWidget {
children: <Widget>[
Text('Service'),
Text('0x${service.uuid.toString().toUpperCase().substring(4, 8)}',
style: Theme.of(context)
.textTheme
.body1
.copyWith(color: Theme.of(context).textTheme.caption.color))
style: Theme.of(context).textTheme.body1?.copyWith(
color: Theme.of(context).textTheme.caption?.color))
],
),
children: characteristicTiles,
Expand All @@ -158,14 +154,14 @@ class ServiceTile extends StatelessWidget {
class CharacteristicTile extends StatelessWidget {
final BluetoothCharacteristic characteristic;
final List<DescriptorTile> descriptorTiles;
final VoidCallback onReadPressed;
final VoidCallback onWritePressed;
final VoidCallback onNotificationPressed;
final VoidCallback? onReadPressed;
final VoidCallback? onWritePressed;
final VoidCallback? onNotificationPressed;

const CharacteristicTile(
{Key key,
this.characteristic,
this.descriptorTiles,
{Key? key,
required this.characteristic,
required this.descriptorTiles,
this.onReadPressed,
this.onWritePressed,
this.onNotificationPressed})
Expand All @@ -187,8 +183,8 @@ class CharacteristicTile extends StatelessWidget {
Text('Characteristic'),
Text(
'0x${characteristic.uuid.toString().toUpperCase().substring(4, 8)}',
style: Theme.of(context).textTheme.body1.copyWith(
color: Theme.of(context).textTheme.caption.color))
style: Theme.of(context).textTheme.body1?.copyWith(
color: Theme.of(context).textTheme.caption?.color))
],
),
subtitle: Text(value.toString()),
Expand All @@ -200,21 +196,21 @@ class CharacteristicTile extends StatelessWidget {
IconButton(
icon: Icon(
Icons.file_download,
color: Theme.of(context).iconTheme.color.withOpacity(0.5),
color: Theme.of(context).iconTheme.color?.withOpacity(0.5),
),
onPressed: onReadPressed,
),
IconButton(
icon: Icon(Icons.file_upload,
color: Theme.of(context).iconTheme.color.withOpacity(0.5)),
color: Theme.of(context).iconTheme.color?.withOpacity(0.5)),
onPressed: onWritePressed,
),
IconButton(
icon: Icon(
characteristic.isNotifying
? Icons.sync_disabled
: Icons.sync,
color: Theme.of(context).iconTheme.color.withOpacity(0.5)),
color: Theme.of(context).iconTheme.color?.withOpacity(0.5)),
onPressed: onNotificationPressed,
)
],
Expand All @@ -228,11 +224,14 @@ class CharacteristicTile extends StatelessWidget {

class DescriptorTile extends StatelessWidget {
final BluetoothDescriptor descriptor;
final VoidCallback onReadPressed;
final VoidCallback onWritePressed;
final VoidCallback? onReadPressed;
final VoidCallback? onWritePressed;

const DescriptorTile(
{Key key, this.descriptor, this.onReadPressed, this.onWritePressed})
{Key? key,
required this.descriptor,
this.onReadPressed,
this.onWritePressed})
: super(key: key);

@override
Expand All @@ -247,7 +246,7 @@ class DescriptorTile extends StatelessWidget {
style: Theme.of(context)
.textTheme
.body1
.copyWith(color: Theme.of(context).textTheme.caption.color))
?.copyWith(color: Theme.of(context).textTheme.caption?.color))
],
),
subtitle: StreamBuilder<List<int>>(
Expand All @@ -261,14 +260,14 @@ class DescriptorTile extends StatelessWidget {
IconButton(
icon: Icon(
Icons.file_download,
color: Theme.of(context).iconTheme.color.withOpacity(0.5),
color: Theme.of(context).iconTheme.color?.withOpacity(0.5),
),
onPressed: onReadPressed,
),
IconButton(
icon: Icon(
Icons.file_upload,
color: Theme.of(context).iconTheme.color.withOpacity(0.5),
color: Theme.of(context).iconTheme.color?.withOpacity(0.5),
),
onPressed: onWritePressed,
)
Expand All @@ -279,7 +278,7 @@ class DescriptorTile extends StatelessWidget {
}

class AdapterStateTile extends StatelessWidget {
const AdapterStateTile({Key key, @required this.state}) : super(key: key);
const AdapterStateTile({Key? key, required this.state}) : super(key: key);

final BluetoothState state;

Expand All @@ -294,7 +293,7 @@ class AdapterStateTile extends StatelessWidget {
),
trailing: Icon(
Icons.error,
color: Theme.of(context).primaryTextTheme.subhead.color,
color: Theme.of(context).primaryTextTheme.subhead?.color,
),
),
);
Expand Down
5 changes: 1 addition & 4 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Demonstrates how to use the flutter_blue plugin.
publish_to: "none"

environment:
sdk: ">=2.1.0 <3.0.0"
sdk: '>=2.12.0 <3.0.0'

dependencies:
flutter:
Expand All @@ -14,9 +14,6 @@ dev_dependencies:
sdk: flutter
flutter_blue:
path: ../
e2e: ^0.2.1
flutter_driver:
sdk: flutter

flutter:
uses-material-design: true
Loading

0 comments on commit 8d8d954

Please sign in to comment.