1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-08 13:58:47 +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

@@ -39,9 +39,7 @@ class DemoInfo {
final String demoTitle;
final String demoRoute;
DemoInfo(this.demoTitle, this.demoRoute)
: assert(demoTitle != null),
assert(demoRoute != null);
DemoInfo(this.demoTitle, this.demoRoute);
}
List<DemoInfo> demoList = [

View File

@@ -64,7 +64,7 @@ class _AddPetDetailsState extends State<AddPetDetails> {
groupValue: petType,
onChanged: (value) {
setState(() {
petType = value;
petType = value!;
});
},
),
@@ -74,7 +74,7 @@ class _AddPetDetailsState extends State<AddPetDetails> {
groupValue: petType,
onChanged: (value) {
setState(() {
petType = value;
petType = value!;
});
},
),

View File

@@ -14,16 +14,16 @@ class Counter {
static MethodChannel methodChannel = const MethodChannel('methodChannelDemo');
/// This method is responsible to increment and return the value of count.
static Future<int> increment({int counterValue}) async {
static Future<int> increment({required int counterValue}) async {
final result = await methodChannel
.invokeMethod<int>('increment', {'count': counterValue});
return result;
return result!;
}
/// This method is responsible to decrement and return the value of count.
static Future<int> decrement({int counterValue}) async {
static Future<int> decrement({required int counterValue}) async {
final result = await methodChannel
.invokeMethod<int>('decrement', {'count': counterValue});
return result;
return result!;
}
}

View File

@@ -26,21 +26,21 @@ class EventChannelDemo extends StatelessWidget {
stream: Accelerometer.readings,
builder: (context, snapshot) {
if (snapshot.hasError) {
return Text((snapshot.error as PlatformException).message);
return Text((snapshot.error as PlatformException).message!);
} else if (snapshot.hasData) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'x axis: ' + snapshot.data.x.toStringAsFixed(3),
'x axis: ' + snapshot.data!.x.toStringAsFixed(3),
style: textStyle,
),
Text(
'y axis: ' + snapshot.data.y.toStringAsFixed(3),
'y axis: ' + snapshot.data!.y.toStringAsFixed(3),
style: textStyle,
),
Text(
'z axis: ' + snapshot.data.z.toStringAsFixed(3),
'z axis: ' + snapshot.data!.z.toStringAsFixed(3),
style: textStyle,
)
],

View File

@@ -15,7 +15,7 @@ class PlatformImageFetcher {
/// Method responsible for providing the platform image.
static Future<Uint8List> getImage() async {
final reply = await _basicMessageChannel.send('getImage') as Uint8List;
final reply = await _basicMessageChannel.send('getImage') as Uint8List?;
if (reply == null) {
throw PlatformException(
code: 'Error',

View File

@@ -3,6 +3,7 @@
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:platform_channels/src/counter_method_channel.dart';
/// The widget demonstrates how to use [MethodChannel] to invoke platform methods.
@@ -45,7 +46,7 @@ class _MethodChannelDemoState extends State<MethodChannelDemo> {
} catch (error) {
showErrorMessage(
context,
error.message as String,
(error as PlatformException).message!,
);
}
},
@@ -63,7 +64,7 @@ class _MethodChannelDemoState extends State<MethodChannelDemo> {
} catch (error) {
showErrorMessage(
context,
error.message as String,
(error as PlatformException).message!,
);
}
},

View File

@@ -44,7 +44,7 @@ class PetListMessageChannel {
/// A model class that provides [petList] which is received from platform.
class PetListModel {
PetListModel({
this.petList,
required this.petList,
});
final List<PetDetails> petList;
@@ -65,8 +65,8 @@ class PetListModel {
/// A simple model that provides pet details like [petType] and [breed] of pet.
class PetDetails {
PetDetails({
this.petType,
this.breed,
required this.petType,
required this.breed,
});
final String petType;

View File

@@ -14,7 +14,7 @@ class PetListScreen extends StatefulWidget {
}
class _PetListScreenState extends State<PetListScreen> {
PetListModel petListModel;
PetListModel petListModel = PetListModel(petList: []);
final scaffoldKey = GlobalKey<ScaffoldState>();
@override
@@ -22,7 +22,7 @@ class _PetListScreenState extends State<PetListScreen> {
super.initState();
// Receives a string of json object from the platform and converts it
// to PetModel.
const BasicMessageChannel('stringCodecDemo', StringCodec())
const BasicMessageChannel<String?>('stringCodecDemo', StringCodec())
.setMessageHandler((message) async {
if (message == null) {
showSnackBar('An error occurred while adding pet details.', context);
@@ -31,7 +31,7 @@ class _PetListScreenState extends State<PetListScreen> {
petListModel = PetListModel.fromJson(message);
});
}
return;
return null;
});
}
@@ -48,7 +48,7 @@ class _PetListScreenState extends State<PetListScreen> {
Navigator.pushNamed(context, '/addPetDetails');
},
),
body: petListModel?.petList?.isEmpty ?? true
body: petListModel.petList.isEmpty
? const Center(child: Text('Enter Pet Details'))
: BuildPetList(petListModel.petList),
);
@@ -79,7 +79,7 @@ class BuildPetList extends StatelessWidget {
await PetListMessageChannel.removePet(index);
showSnackBar('Removed successfully!', context);
} catch (error) {
showSnackBar(error.message.toString(), context);
showSnackBar((error as PlatformException).message!, context);
}
},
),

View File

@@ -18,7 +18,7 @@ class PlatformImageDemo extends StatefulWidget {
}
class _PlatformImageDemoState extends State<PlatformImageDemo> {
Future<Uint8List> imageData;
Future<Uint8List>? imageData;
@override
Widget build(BuildContext context) {
@@ -41,12 +41,14 @@ class _PlatformImageDemoState extends State<PlatformImageDemo> {
return const Placeholder();
} else if (snapshot.hasError) {
return Center(
child: Text(snapshot.error.toString()),
child: Text(
(snapshot.error as PlatformException).message!,
),
);
} else if (snapshot.connectionState ==
ConnectionState.done) {
return Image.memory(
snapshot.data,
snapshot.data!,
fit: BoxFit.fill,
);
}