Skip to content

Commit

Permalink
Refactored FlutterMapState's maybeOf method (#1495)
Browse files Browse the repository at this point in the history
* Refactored `FlutterMapState`'s `maybeOf` method into itself and `of` to meet good practise guidelines

* Removed unnecessary duplication
  • Loading branch information
JaffaKetchup authored Apr 21, 2023
1 parent a051edb commit 8c49bfa
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion example/lib/pages/scale_layer_plugin_option.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion example/lib/pages/zoombuttons_plugin_option.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion lib/src/layer/attribution_layer/rich.dart
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class RichAttributionWidgetState extends State<RichAttributionWidget> {
context,
() {
setState(() => popupExpanded = true);
mapEventSubscription = FlutterMapState.maybeOf(context)!
mapEventSubscription = FlutterMapState.of(context)
.mapController
.mapEventStream
.listen((e) {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/layer/circle_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <Widget>[];
for (final circle in circles) {
circle.offset = map.getOffsetFromOrigin(circle.point);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/layer/marker_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <Widget>[];

for (final marker in markers) {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/layer/overlay_image_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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: <Widget>[
Expand Down
2 changes: 1 addition & 1 deletion lib/src/layer/polygon_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Polygon> pgons = polygonCulling
Expand Down
2 changes: 1 addition & 1 deletion lib/src/layer/polyline_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Polyline> lines = polylineCulling
Expand Down
2 changes: 1 addition & 1 deletion lib/src/layer/tile_layer/tile_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ class _TileLayerState extends State<TileLayer> 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());
Expand Down
17 changes: 8 additions & 9 deletions lib/src/map/flutter_map_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<MapStateInheritedWidget>();
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<MapStateInheritedWidget>()
?.mapState;

static FlutterMapState of(BuildContext context) =>
maybeOf(context) ??
(throw StateError(
'`FlutterMapState.of()` should not be called outside a `FlutterMap` and its children'));
}

class _SafeArea {
Expand Down

0 comments on commit 8c49bfa

Please sign in to comment.