Skip to content
This repository was archived by the owner on Jul 11, 2021. It is now read-only.

Commit 9f07424

Browse files
authored
Migrate to null safety (#101)
1 parent d9fc7a3 commit 9f07424

7 files changed

+58
-57
lines changed

example/lib/main.dart

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import 'dart:async';
33
import 'package:flutter/material.dart';
44
import 'package:user_location/user_location.dart';
55
import 'package:flutter_map/flutter_map.dart';
6-
import 'package:latlong/latlong.dart';
6+
import 'package:latlong2/latlong.dart';
77

88
void main() => runApp(MyApp());
99

@@ -21,10 +21,10 @@ class MyApp extends StatelessWidget {
2121
}
2222

2323
class HomePage extends StatelessWidget {
24-
MapController mapController = MapController();
25-
List<Marker> markers = [];
26-
StreamController<LatLng> markerlocationStream = StreamController();
27-
UserLocationOptions userLocationOptions;
24+
final MapController mapController = MapController();
25+
final List<Marker> markers = [];
26+
final StreamController<LatLng> markerlocationStream = StreamController();
27+
late UserLocationOptions userLocationOptions;
2828

2929
onTapFAB() {
3030
print('Callback function has been called');
@@ -42,7 +42,7 @@ class HomePage extends StatelessWidget {
4242
context: context,
4343
mapController: mapController,
4444
markers: markers,
45-
onLocationUpdate: (LatLng pos, double speed) =>
45+
onLocationUpdate: (LatLng pos, double? speed) =>
4646
print("onLocationUpdate ${pos.toString()}"),
4747
updateMapLocationOnPositionChange: false,
4848
showMoveToCurrentLocationFloatingActionButton: true,

example/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ publish_to: 'none'
55
version: 1.0.0
66

77
environment:
8-
sdk: ">=2.1.0 <3.0.0"
8+
sdk: '>=2.12.0 <3.0.0'
99

1010
dependencies:
1111
flutter:

example/test/widget_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void main() {
1919
expect(
2020
find.byWidgetPredicate(
2121
(Widget widget) => widget is Text &&
22-
widget.data.startsWith('Running on:'),
22+
widget.data!.startsWith('Running on:'),
2323
),
2424
findsOneWidget,
2525
);

lib/src/user_location_layer.dart

+34-33
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import 'dart:async';
22
import 'dart:io';
33
import 'dart:math' as math;
4-
import "dart:math" show pi;
54

65
import 'package:flutter/material.dart';
76
import 'package:flutter/services.dart';
87
import 'package:flutter/widgets.dart';
98
import 'package:flutter_compass/flutter_compass.dart';
109
import 'package:flutter_map/plugin_api.dart';
11-
import 'package:latlong/latlong.dart';
10+
import 'package:latlong2/latlong.dart';
1211
import 'package:location/location.dart';
1312
import 'package:user_location/src/user_location_marker.dart';
1413
import 'package:user_location/src/user_location_options.dart';
@@ -26,24 +25,24 @@ class MapsPluginLayer extends StatefulWidget {
2625

2726
class _MapsPluginLayerState extends State<MapsPluginLayer>
2827
with TickerProviderStateMixin, WidgetsBindingObserver {
29-
LatLng _currentLocation;
30-
UserLocationMarker _locationMarker;
28+
LatLng? _currentLocation;
29+
UserLocationMarker? _locationMarker;
3130
EventChannel _stream = EventChannel('locationStatusStream');
3231
var location = Location();
3332

34-
bool mapLoaded;
35-
bool initialStateOfupdateMapLocationOnPositionChange;
33+
late bool mapLoaded;
34+
late bool initialStateOfupdateMapLocationOnPositionChange;
3635

37-
double _direction;
36+
double? _direction;
3837

39-
StreamSubscription<LocationData> _onLocationChangedStreamSubscription;
40-
StreamSubscription<CompassEvent> _compassStreamSubscription;
41-
StreamSubscription _locationStatusChangeSubscription;
38+
StreamSubscription<LocationData>? _onLocationChangedStreamSubscription;
39+
StreamSubscription<CompassEvent>? _compassStreamSubscription;
40+
StreamSubscription? _locationStatusChangeSubscription;
4241

4342
@override
4443
void initState() {
4544
super.initState();
46-
WidgetsBinding.instance.addObserver(this);
45+
WidgetsBinding.instance!.addObserver(this);
4746

4847
initialStateOfupdateMapLocationOnPositionChange =
4948
widget.options.updateMapLocationOnPositionChange;
@@ -56,7 +55,7 @@ class _MapsPluginLayerState extends State<MapsPluginLayer>
5655

5756
@override
5857
void dispose() {
59-
WidgetsBinding.instance.removeObserver(this);
58+
WidgetsBinding.instance!.removeObserver(this);
6059
_locationStatusChangeSubscription?.cancel();
6160
_onLocationChangedStreamSubscription?.cancel();
6261
_compassStreamSubscription?.cancel();
@@ -140,11 +139,11 @@ class _MapsPluginLayerState extends State<MapsPluginLayer>
140139
if (onValue.latitude == null || onValue.longitude == null) {
141140
_currentLocation = LatLng(0, 0);
142141
} else {
143-
_currentLocation = LatLng(onValue.latitude, onValue.longitude);
142+
_currentLocation = LatLng(onValue.latitude!, onValue.longitude!);
144143
}
145144

146-
var height = 20.0 * (1 - (onValue.accuracy / 100));
147-
var width = 20.0 * (1 - (onValue.accuracy / 100));
145+
var height = 20.0 * (1 - (onValue.accuracy! / 100));
146+
var width = 20.0 * (1 - (onValue.accuracy! / 100));
148147
if (height < 0 || width < 0) {
149148
height = 20;
150149
width = 20;
@@ -160,15 +159,15 @@ class _MapsPluginLayerState extends State<MapsPluginLayer>
160159
height: 20.0,
161160
width: 20.0,
162161
point:
163-
LatLng(_currentLocation.latitude, _currentLocation.longitude),
162+
LatLng(_currentLocation!.latitude, _currentLocation!.longitude),
164163
builder: (context) {
165164
return Stack(
166165
alignment: AlignmentDirectional.center,
167166
children: <Widget>[
168167
if (_direction != null && widget.options.showHeading)
169168
ClipOval(
170169
child: Transform.rotate(
171-
angle: _direction / 180 * math.pi,
170+
angle: _direction! / 180 * math.pi,
172171
child: CustomPaint(
173172
size: Size(60.0, 60.0),
174173
painter: MyDirectionPainter(),
@@ -185,7 +184,7 @@ class _MapsPluginLayerState extends State<MapsPluginLayer>
185184
width: 20,
186185
decoration: BoxDecoration(
187186
shape: BoxShape.circle,
188-
color: Colors.blue[300].withOpacity(0.7),
187+
color: Colors.blue[300]!.withOpacity(0.7),
189188
),
190189
),
191190
),
@@ -226,20 +225,22 @@ class _MapsPluginLayerState extends State<MapsPluginLayer>
226225
setState(() {
227226
mapLoaded = true;
228227
});
229-
animatedMapMove(_currentLocation, widget.options.defaultZoom,
228+
animatedMapMove(_currentLocation!, widget.options.defaultZoom,
230229
widget.options.mapController, this);
231230
}
232231
});
233232
});
234233
}
235234
}
236235

237-
void _moveMapToCurrentLocation({double zoom}) {
236+
void _moveMapToCurrentLocation({double? zoom}) {
238237
if (_currentLocation != null) {
239238
animatedMapMove(
240-
LatLng(_currentLocation.latitude ?? LatLng(0, 0),
241-
_currentLocation.longitude ?? LatLng(0, 0)),
242-
zoom ?? widget.map.zoom ?? 15,
239+
LatLng(
240+
_currentLocation!.latitude,
241+
_currentLocation!.longitude,
242+
),
243+
zoom ?? widget.map.zoom,
243244
widget.options.mapController,
244245
this,
245246
);
@@ -248,7 +249,7 @@ class _MapsPluginLayerState extends State<MapsPluginLayer>
248249

249250
void _handleLocationStatusChanges() {
250251
printLog(_stream.toString());
251-
bool _locationStatusChanged;
252+
bool? _locationStatusChanged;
252253
if (_locationStatusChanged == null) {
253254
_locationStatusChangeSubscription =
254255
_stream.receiveBroadcastStream().listen((onData) {
@@ -267,7 +268,7 @@ class _MapsPluginLayerState extends State<MapsPluginLayer>
267268

268269
void _handleCompassDirection() {
269270
if (widget.options.showHeading) {
270-
_compassStreamSubscription = FlutterCompass.events.listen((event) {
271+
_compassStreamSubscription = FlutterCompass.events!.listen((event) {
271272
setState(() {
272273
_direction = event.heading;
273274
});
@@ -280,8 +281,8 @@ class _MapsPluginLayerState extends State<MapsPluginLayer>
280281
if (widget.options.onLocationUpdate == null) {
281282
printLog("Stream not provided");
282283
} else {
283-
widget.options.onLocationUpdate(
284-
LatLng(onValue.latitude, onValue.longitude),
284+
widget.options.onLocationUpdate!(
285+
LatLng(onValue.latitude!, onValue.longitude!),
285286
onValue.speed,
286287
);
287288
}
@@ -313,7 +314,7 @@ class _MapsPluginLayerState extends State<MapsPluginLayer>
313314
initialize();
314315
_moveMapToCurrentLocation(zoom: widget.options.defaultZoom);
315316
if (widget.options.onTapFAB != null) {
316-
widget.options.onTapFAB();
317+
widget.options.onTapFAB!();
317318
}
318319
},
319320
child: widget.options.moveToCurrentLocationFloatingActionButton ??
@@ -381,11 +382,11 @@ class _MapsPluginLayerState extends State<MapsPluginLayer>
381382
}
382383

383384
void forceMapUpdate() {
384-
var zoom = widget.options.mapController.zoom;
385-
widget.options.mapController.move(widget.options.mapController.center,
386-
widget.options.mapController.zoom + 0.000001);
387-
widget.options.mapController
388-
.move(widget.options.mapController.center, zoom);
385+
var zoom = widget.options.mapController!.zoom;
386+
widget.options.mapController!.move(widget.options.mapController!.center,
387+
widget.options.mapController!.zoom + 0.000001);
388+
widget.options.mapController!
389+
.move(widget.options.mapController!.center, zoom);
389390
}
390391
}
391392

lib/src/user_location_marker.dart

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import 'package:flutter_map/flutter_map.dart';
22

33
class UserLocationMarker extends Marker {
44
UserLocationMarker({
5-
point,
6-
builder,
7-
width,
8-
height,
9-
AnchorPos anchorPos,
5+
required point,
6+
required builder,
7+
required width,
8+
required height,
9+
AnchorPos? anchorPos,
1010
}) : super(
1111
point: point,
1212
builder: builder,

lib/src/user_location_options.dart

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter_map/plugin_api.dart';
3-
import 'package:latlong/latlong.dart';
3+
import 'package:latlong2/latlong.dart';
44

55
class UserLocationOptions extends LayerOptions {
66
BuildContext context;
7-
List<Marker> markers;
8-
MapController mapController;
7+
List<Marker?> markers;
8+
MapController? mapController;
99

10-
Widget markerWidget;
10+
Widget? markerWidget;
1111
bool updateMapLocationOnPositionChange;
1212
bool showMoveToCurrentLocationFloatingActionButton;
1313
bool zoomToCurrentLocationOnLoad;
14-
Widget moveToCurrentLocationFloatingActionButton;
14+
Widget? moveToCurrentLocationFloatingActionButton;
1515

16-
void Function(LatLng location, double speed) onLocationUpdate;
17-
Function() onTapFAB;
16+
void Function(LatLng location, double? speed)? onLocationUpdate;
17+
Function()? onTapFAB;
1818

1919
double fabBottom;
2020
double fabRight;
@@ -37,8 +37,8 @@ class UserLocationOptions extends LayerOptions {
3737
int locationUpdateIntervalMs;
3838

3939
UserLocationOptions(
40-
{@required this.context,
41-
@required this.markers,
40+
{required this.context,
41+
required this.markers,
4242
this.mapController,
4343
this.markerWidget,
4444
this.onLocationUpdate,

pubspec.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ author: Gaurab Panthee <[email protected]>
55
homepage: https://github.com/igaurab/UserLocationPlugin
66

77
environment:
8-
sdk: ">=2.3.0 <3.0.0"
8+
sdk: '>=2.12.0 <3.0.0'
99

1010
dependencies:
1111
flutter:
1212
sdk: flutter
1313
location: ^4.1.1
14-
flutter_map: ^0.12.0
14+
flutter_map: ^0.13.0
1515
flutter_compass: ^0.6.0
1616

1717
dev_dependencies:

0 commit comments

Comments
 (0)