1
0
mirror of https://github.com/flutter/samples.git synced 2026-03-28 15:21:30 +00:00

[platform_channels] Migrate to sound null safety (#803)

This commit is contained in:
Ayush Bherwani
2021-06-08 04:00:12 +05:30
committed by GitHub
parent c8112c88ac
commit 865c545f3d
15 changed files with 50 additions and 46 deletions

View File

@@ -19,8 +19,8 @@ void main() {
// calls the BinaryMessenger.setMessageHandler registered for the EventChannel
// and add the incoming message to the StreamController used by the EventChannel
// after decoding the message with codec used by the EventChannel.
void emitValues(ByteData event) {
ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage(
void emitValues(ByteData? event) {
ServicesBinding.instance?.defaultBinaryMessenger.handlePlatformMessage(
'eventChannelDemo',
event,
(reply) {},
@@ -29,7 +29,7 @@ void main() {
// Register a mock for EventChannel. EventChannel under the hood uses
// MethodChannel to listen and cancel the created stream.
ServicesBinding.instance.defaultBinaryMessenger
ServicesBinding.instance?.defaultBinaryMessenger
.setMockMessageHandler('eventChannelDemo', (message) async {
// Decode the message into MethodCallHandler.
final methodCall = standardMethod.decodeMethodCall(message);

View File

@@ -13,7 +13,7 @@ import 'package:platform_channels/src/pet_list_screen.dart';
void main() {
group('PetListScreen tests', () {
const basicMessageChannel =
BasicMessageChannel('stringCodecDemo', StringCodec());
BasicMessageChannel<String?>('stringCodecDemo', StringCodec());
var petList = [
{
@@ -22,21 +22,21 @@ void main() {
}
];
PetListModel petListModel;
PetListModel? petListModel;
setUpAll(() {
// Mock for the pet list received from the platform.
basicMessageChannel.setMockMessageHandler((message) async {
petListModel = PetListModel.fromJson(message);
return;
petListModel = PetListModel.fromJson(message!);
return null;
});
// Mock for the index received from the Dart to delete the pet details,
// and send the updated pet list back to Dart.
const BasicMessageChannel('binaryCodecDemo', BinaryCodec())
const BasicMessageChannel<ByteData?>('binaryCodecDemo', BinaryCodec())
.setMockMessageHandler((message) async {
// Convert the ByteData to String.
final index = utf8.decoder.convert(message.buffer
final index = utf8.decoder.convert(message!.buffer
.asUint8List(message.offsetInBytes, message.lengthInBytes));
// Remove the pet details at the given index.
@@ -45,7 +45,8 @@ void main() {
// Send the updated petList back.
final map = {'petList': petList};
await basicMessageChannel.send(json.encode(map));
return;
return null;
});
});
@@ -60,7 +61,7 @@ void main() {
basicMessageChannel.send(json.encode(map));
// Get the details of first pet.
final petDetails = petListModel.petList.first;
final petDetails = petListModel!.petList.first;
expect(petDetails.petType, 'Dog');
expect(petDetails.breed, 'Pug');
});
@@ -68,7 +69,7 @@ void main() {
testWidgets('BuildPetList test', (tester) async {
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: BuildPetList(petListModel.petList),
body: BuildPetList(petListModel!.petList),
),
));
@@ -77,7 +78,7 @@ void main() {
// Delete the pet details.
await tester.tap(find.byIcon(Icons.delete).first);
expect(petListModel.petList, isEmpty);
expect(petListModel!.petList, isEmpty);
});
});
}