From 8c49bfa1d9bdd2b1c1e9ce4ba01660ba12a2d8ef Mon Sep 17 00:00:00 2001 From: Luka S Date: Fri, 21 Apr 2023 18:57:31 +0100 Subject: [PATCH] Refactored `FlutterMapState`'s `maybeOf` method (#1495) * Refactored `FlutterMapState`'s `maybeOf` method into itself and `of` to meet good practise guidelines * Removed unnecessary duplication --- .../lib/pages/scale_layer_plugin_option.dart | 2 +- .../lib/pages/zoombuttons_plugin_option.dart | 2 +- lib/src/layer/attribution_layer/rich.dart | 2 +- lib/src/layer/circle_layer.dart | 2 +- lib/src/layer/marker_layer.dart | 2 +- lib/src/layer/overlay_image_layer.dart | 2 +- lib/src/layer/polygon_layer.dart | 2 +- lib/src/layer/polyline_layer.dart | 2 +- lib/src/layer/tile_layer/tile_layer.dart | 2 +- lib/src/map/flutter_map_state.dart | 17 ++++++++--------- 10 files changed, 17 insertions(+), 18 deletions(-) diff --git a/example/lib/pages/scale_layer_plugin_option.dart b/example/lib/pages/scale_layer_plugin_option.dart index 013a0a06c..3ea452187 100644 --- a/example/lib/pages/scale_layer_plugin_option.dart +++ b/example/lib/pages/scale_layer_plugin_option.dart @@ -52,7 +52,7 @@ class ScaleLayerWidget extends StatelessWidget { @override Widget build(BuildContext context) { - final map = FlutterMapState.maybeOf(context)!; + final map = FlutterMapState.of(context); final zoom = map.zoom; final distance = scale[max(0, min(20, zoom.round() + 2))].toDouble(); final center = map.center; diff --git a/example/lib/pages/zoombuttons_plugin_option.dart b/example/lib/pages/zoombuttons_plugin_option.dart index 37241f715..1bb102fdc 100644 --- a/example/lib/pages/zoombuttons_plugin_option.dart +++ b/example/lib/pages/zoombuttons_plugin_option.dart @@ -34,7 +34,7 @@ class FlutterMapZoomButtons extends StatelessWidget { @override Widget build(BuildContext context) { - final map = FlutterMapState.maybeOf(context)!; + final map = FlutterMapState.of(context); return Align( alignment: alignment, child: Column( diff --git a/lib/src/layer/attribution_layer/rich.dart b/lib/src/layer/attribution_layer/rich.dart index 3a7183593..29ef73b9f 100644 --- a/lib/src/layer/attribution_layer/rich.dart +++ b/lib/src/layer/attribution_layer/rich.dart @@ -235,7 +235,7 @@ class RichAttributionWidgetState extends State { context, () { setState(() => popupExpanded = true); - mapEventSubscription = FlutterMapState.maybeOf(context)! + mapEventSubscription = FlutterMapState.of(context) .mapController .mapEventStream .listen((e) { diff --git a/lib/src/layer/circle_layer.dart b/lib/src/layer/circle_layer.dart index f06ea4014..a8b736c18 100644 --- a/lib/src/layer/circle_layer.dart +++ b/lib/src/layer/circle_layer.dart @@ -36,7 +36,7 @@ class CircleLayer extends StatelessWidget { return LayoutBuilder( builder: (BuildContext context, BoxConstraints bc) { final size = Size(bc.maxWidth, bc.maxHeight); - final map = FlutterMapState.maybeOf(context)!; + final map = FlutterMapState.of(context); final circleWidgets = []; for (final circle in circles) { circle.offset = map.getOffsetFromOrigin(circle.point); diff --git a/lib/src/layer/marker_layer.dart b/lib/src/layer/marker_layer.dart index 335eeb55d..265b0268f 100644 --- a/lib/src/layer/marker_layer.dart +++ b/lib/src/layer/marker_layer.dart @@ -159,7 +159,7 @@ class MarkerLayer extends StatelessWidget { @override Widget build(BuildContext context) { - final map = FlutterMapState.maybeOf(context)!; + final map = FlutterMapState.of(context); final markerWidgets = []; for (final marker in markers) { diff --git a/lib/src/layer/overlay_image_layer.dart b/lib/src/layer/overlay_image_layer.dart index 7d6390d8c..5850d839b 100644 --- a/lib/src/layer/overlay_image_layer.dart +++ b/lib/src/layer/overlay_image_layer.dart @@ -136,7 +136,7 @@ class OverlayImageLayer extends StatelessWidget { @override Widget build(BuildContext context) { - final map = FlutterMapState.maybeOf(context)!; + final map = FlutterMapState.of(context); return ClipRect( child: Stack( children: [ diff --git a/lib/src/layer/polygon_layer.dart b/lib/src/layer/polygon_layer.dart index 51718fe51..f81dc878f 100644 --- a/lib/src/layer/polygon_layer.dart +++ b/lib/src/layer/polygon_layer.dart @@ -78,7 +78,7 @@ class PolygonLayer extends StatelessWidget { @override Widget build(BuildContext context) { - final map = FlutterMapState.maybeOf(context)!; + final map = FlutterMapState.of(context); final size = Size(map.size.x, map.size.y); final List pgons = polygonCulling diff --git a/lib/src/layer/polyline_layer.dart b/lib/src/layer/polyline_layer.dart index 898d20b11..96c11bb2d 100644 --- a/lib/src/layer/polyline_layer.dart +++ b/lib/src/layer/polyline_layer.dart @@ -77,7 +77,7 @@ class PolylineLayer extends StatelessWidget { @override Widget build(BuildContext context) { - final map = FlutterMapState.maybeOf(context)!; + final map = FlutterMapState.of(context); final size = Size(map.size.x, map.size.y); final List lines = polylineCulling diff --git a/lib/src/layer/tile_layer/tile_layer.dart b/lib/src/layer/tile_layer/tile_layer.dart index 07bb4fddc..96f7def19 100644 --- a/lib/src/layer/tile_layer/tile_layer.dart +++ b/lib/src/layer/tile_layer/tile_layer.dart @@ -411,7 +411,7 @@ class _TileLayerState extends State with TickerProviderStateMixin { @override Widget build(BuildContext context) { - final map = FlutterMapState.maybeOf(context)!; + final map = FlutterMapState.of(context); //Handle movement final tileZoom = _clampZoom(map.zoom.roundToDouble()); diff --git a/lib/src/map/flutter_map_state.dart b/lib/src/map/flutter_map_state.dart index b64b39e62..c40016241 100644 --- a/lib/src/map/flutter_map_state.dart +++ b/lib/src/map/flutter_map_state.dart @@ -756,15 +756,14 @@ class FlutterMapState extends MapGestureMixin double _calculateScreenHeightInDegrees() => options.screenSize!.height * 170.102258 / math.pow(2, zoom + 8); - static FlutterMapState? maybeOf(BuildContext context, {bool nullOk = false}) { - final widget = - context.dependOnInheritedWidgetOfExactType(); - if (nullOk || widget != null) { - return widget?.mapState; - } - throw FlutterError( - 'MapState.of() called with a context that does not contain a FlutterMap.'); - } + static FlutterMapState? maybeOf(BuildContext context) => context + .dependOnInheritedWidgetOfExactType() + ?.mapState; + + static FlutterMapState of(BuildContext context) => + maybeOf(context) ?? + (throw StateError( + '`FlutterMapState.of()` should not be called outside a `FlutterMap` and its children')); } class _SafeArea {