Skip to content

Commit 654e66c

Browse files
authored
Update examples to null safety (#732)
* Make ReadMe examples null-safe & fix errors. * Make example project null safe. * Fix most linter warnings.
1 parent 1db8bfe commit 654e66c

10 files changed

+157
-128
lines changed

README.md

+34-28
Original file line numberDiff line numberDiff line change
@@ -479,40 +479,41 @@ Unable to find a matching variant of project :unityLibrary:
479479

480480
```dart
481481
import 'package:flutter/material.dart';
482-
import 'package:flutter/services.dart';
483482
import 'package:flutter_unity_widget/flutter_unity_widget.dart';
484483
485484
void main() {
486-
runApp(MaterialApp(
487-
home: UnityDemoScreen()
488-
));
485+
runApp(
486+
const MaterialApp(
487+
home: UnityDemoScreen(),
488+
),
489+
);
489490
}
490491
491492
class UnityDemoScreen extends StatefulWidget {
492-
493-
UnityDemoScreen({Key key}) : super(key: key);
493+
const UnityDemoScreen({Key? key}) : super(key: key);
494494
495495
@override
496-
_UnityDemoScreenState createState() => _UnityDemoScreenState();
496+
State<UnityDemoScreen> createState() => _UnityDemoScreenState();
497497
}
498498
499-
class _UnityDemoScreenState extends State<UnityDemoScreen>{
499+
class _UnityDemoScreenState extends State<UnityDemoScreen> {
500500
static final GlobalKey<ScaffoldState> _scaffoldKey =
501501
GlobalKey<ScaffoldState>();
502-
UnityWidgetController _unityWidgetController;
502+
UnityWidgetController? _unityWidgetController;
503503
504+
@override
504505
Widget build(BuildContext context) {
505-
506506
return Scaffold(
507507
key: _scaffoldKey,
508508
body: SafeArea(
509509
bottom: false,
510510
child: WillPopScope(
511-
onWillPop: () {
511+
onWillPop: () async {
512512
// Pop the category page if Android back button is pressed.
513+
return true;
513514
},
514515
child: Container(
515-
color: colorYellow,
516+
color: Colors.yellow,
516517
child: UnityWidget(
517518
onUnityCreated: onUnityCreated,
518519
),
@@ -524,9 +525,10 @@ class _UnityDemoScreenState extends State<UnityDemoScreen>{
524525
525526
// Callback that connects the created controller to the unity controller
526527
void onUnityCreated(controller) {
527-
this._unityWidgetController = controller;
528+
_unityWidgetController = controller;
528529
}
529530
}
531+
530532
```
531533
<br />
532534

@@ -536,17 +538,19 @@ class _UnityDemoScreenState extends State<UnityDemoScreen>{
536538
import 'package:flutter/material.dart';
537539
import 'package:flutter_unity_widget/flutter_unity_widget.dart';
538540
539-
void main() => runApp(MyApp());
541+
void main() => runApp(const MyApp());
540542
541543
class MyApp extends StatefulWidget {
544+
const MyApp({Key? key}) : super(key: key);
545+
542546
@override
543-
_MyAppState createState() => _MyAppState();
547+
State<MyApp> createState() => _MyAppState();
544548
}
545549
546550
class _MyAppState extends State<MyApp> {
547551
static final GlobalKey<ScaffoldState> _scaffoldKey =
548552
GlobalKey<ScaffoldState>();
549-
UnityWidgetController _unityWidgetController;
553+
UnityWidgetController? _unityWidgetController;
550554
double _sliderValue = 0.0;
551555
552556
@override
@@ -571,10 +575,10 @@ class _MyAppState extends State<MyApp> {
571575
child: Stack(
572576
children: <Widget>[
573577
UnityWidget(
574-
onUnityCreated: onUnityCreated,
575-
onUnityMessage: onUnityMessage,
576-
onUnitySceneLoaded: onUnitySceneLoaded,
577-
fullscreen: false,
578+
onUnityCreated: onUnityCreated,
579+
onUnityMessage: onUnityMessage,
580+
onUnitySceneLoaded: onUnitySceneLoaded,
581+
fullscreen: false,
578582
),
579583
Positioned(
580584
bottom: 20,
@@ -584,8 +588,8 @@ class _MyAppState extends State<MyApp> {
584588
elevation: 10,
585589
child: Column(
586590
children: <Widget>[
587-
Padding(
588-
padding: const EdgeInsets.only(top: 20),
591+
const Padding(
592+
padding: EdgeInsets.only(top: 20),
589593
child: Text("Rotation speed:"),
590594
),
591595
Slider(
@@ -612,7 +616,7 @@ class _MyAppState extends State<MyApp> {
612616
613617
// Communcation from Flutter to Unity
614618
void setRotationSpeed(String speed) {
615-
_unityWidgetController.postMessage(
619+
_unityWidgetController?.postMessage(
616620
'Cube',
617621
'SetRotationSpeed',
618622
speed,
@@ -626,15 +630,17 @@ class _MyAppState extends State<MyApp> {
626630
627631
// Callback that connects the created controller to the unity controller
628632
void onUnityCreated(controller) {
629-
this._unityWidgetController = controller;
633+
_unityWidgetController = controller;
630634
}
631635
632636
// Communication from Unity when new scene is loaded to Flutter
633-
void onUnitySceneLoaded(SceneLoaded sceneInfo) {
634-
print('Received scene loaded from unity: ${sceneInfo.name}');
635-
print('Received scene loaded from unity buildIndex: ${sceneInfo.buildIndex}');
637+
void onUnitySceneLoaded(SceneLoaded? sceneInfo) {
638+
if (sceneInfo != null) {
639+
print('Received scene loaded from unity: ${sceneInfo.name}');
640+
print(
641+
'Received scene loaded from unity buildIndex: ${sceneInfo.buildIndex}');
642+
}
636643
}
637-
638644
}
639645
640646
```

example/lib/main.dart

+9-7
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import 'screens/loader_screen.dart';
88
import 'screens/simple_screen.dart';
99

1010
void main() {
11-
runApp(MyApp());
11+
runApp(const MyApp());
1212
}
1313

1414
class MyApp extends StatelessWidget {
15+
const MyApp({Key? key}) : super(key: key);
16+
1517
// This widget is the root of your application.
1618
@override
1719
Widget build(BuildContext context) {
@@ -23,12 +25,12 @@ class MyApp extends StatelessWidget {
2325
),
2426
initialRoute: '/',
2527
routes: {
26-
'/': (context) => MenuScreen(),
27-
'/simple': (context) => SimpleScreen(),
28-
'/loader': (context) => LoaderScreen(),
29-
'/orientation': (context) => OrientationScreen(),
30-
'/api': (context) => ApiScreen(),
31-
'/none': (context) => NoInteractionScreen(),
28+
'/': (context) => const MenuScreen(),
29+
'/simple': (context) => const SimpleScreen(),
30+
'/loader': (context) => const LoaderScreen(),
31+
'/orientation': (context) => const OrientationScreen(),
32+
'/api': (context) => const ApiScreen(),
33+
'/none': (context) => const NoInteractionScreen(),
3234
},
3335
);
3436
}

example/lib/menu_screen.dart

+21-14
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
11
import 'package:flutter/material.dart';
22

33
class MenuScreen extends StatefulWidget {
4-
MenuScreen({Key key}) : super(key: key);
4+
const MenuScreen({Key? key}) : super(key: key);
55

66
@override
7-
_MenuScreenState createState() => _MenuScreenState();
7+
State<MenuScreen> createState() => _MenuScreenState();
88
}
99

1010
class _MenuScreenState extends State<MenuScreen> {
1111
bool enableAR = true;
1212

1313
List<_MenuListItem> menus = [
14-
new _MenuListItem(
14+
_MenuListItem(
1515
description: 'Simple demonstration of unity flutter library',
1616
route: '/simple',
1717
title: 'Simple Unity Demo',
1818
enableAR: false,
1919
),
20-
new _MenuListItem(
20+
_MenuListItem(
2121
description: 'No interaction of unity flutter library',
2222
route: '/none',
2323
title: 'No Interaction Unity Demo',
2424
enableAR: false,
2525
),
26-
new _MenuListItem(
26+
_MenuListItem(
2727
description: 'Unity load and unload unity demo',
2828
route: '/loader',
2929
title: 'Safe mode Demo',
3030
enableAR: false,
3131
),
32-
new _MenuListItem(
32+
_MenuListItem(
3333
description:
3434
'This example shows various native API exposed by the library',
3535
route: '/api',
3636
title: 'Native exposed API demo',
3737
enableAR: false,
3838
),
39-
new _MenuListItem(
39+
_MenuListItem(
4040
title: 'Test Orientation',
4141
route: '/orientation',
4242
description: 'test orientation change',
4343
enableAR: false,
4444
),
45-
new _MenuListItem(
45+
_MenuListItem(
4646
description: 'Unity native activity demo',
4747
route: '/activity',
4848
title: 'Native Activity Demo ',
@@ -54,17 +54,19 @@ class _MenuScreenState extends State<MenuScreen> {
5454
Widget build(BuildContext context) {
5555
return Scaffold(
5656
appBar: AppBar(
57-
title: Text('Menu List'),
57+
title: const Text('Menu List'),
5858
actions: [
5959
Row(
6060
children: [
61-
Text("Enable AR"),
61+
const Text("Enable AR"),
6262
Checkbox(
6363
value: enableAR,
6464
onChanged: (changed) {
65-
setState(() {
66-
enableAR = changed;
67-
});
65+
if (changed != null) {
66+
setState(() {
67+
enableAR = changed;
68+
});
69+
}
6870
},
6971
),
7072
],
@@ -97,5 +99,10 @@ class _MenuListItem {
9799
final String route;
98100
final bool enableAR;
99101

100-
_MenuListItem({this.title, this.description, this.route, this.enableAR});
102+
_MenuListItem({
103+
required this.title,
104+
required this.description,
105+
required this.route,
106+
required this.enableAR,
107+
});
101108
}

0 commit comments

Comments
 (0)