Skip to content

Commit 9907251

Browse files
authored
Improved code quality (refactoring & stricter linting) (#1594)
* add lints, code style improvements, add assert messages * use documentation comments for methods https://dart.dev/language/comments#documentation-comments * better callback parameter names, dart format * fix lazy fields in `MapCamera` * use documentation comments part 2 * use `@immutable` for immutables, const constructors where possible * refactor loops * use math min() / max() * remove omit_local_variable_types lint * use full method bodies instead of `=>` * typo * Fixed conflict resolution bugs * Fix lint warning * remove cupertino imports fix https://github.com/fleaflet/flutter_map/pull/1594/files/a50be82dfbfc92fff753c7b704d4b98bb63a6d81#r1275362713 https://github.com/fleaflet/flutter_map/pull/1594/files/a50be82dfbfc92fff753c7b704d4b98bb63a6d81#r1275360404 * remove non null cast * revert list mapping https://github.com/fleaflet/flutter_map/pull/1594/files/38d7250da5524779449cab91559d1b25234641da..df4b2053e79c71704b02fd72dcfe52bfd28d6888#r1270268326
1 parent 991f53c commit 9907251

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+561
-464
lines changed

analysis_options.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,19 @@ linter:
2727
type_annotate_public_apis: true
2828
unnecessary_statements: true
2929
use_named_constants: true
30+
use_super_parameters: true
31+
cast_nullable_to_non_nullable: true
32+
only_throw_errors: true
33+
sort_unnamed_constructors_first: true
34+
prefer_asserts_with_message: true
35+
missing_whitespace_between_adjacent_strings: true
36+
avoid_multiple_declarations_per_line: true
37+
noop_primitive_operations: true
38+
avoid_void_async: true
39+
avoid_redundant_argument_values: true
40+
avoid_types_on_closure_parameters: true
41+
unnecessary_null_checks: true
42+
prefer_single_quotes: true
43+
unnecessary_parenthesis: true
44+
use_if_null_to_convert_nulls_to_bools: true
45+
matching_super_parameters: true

example/lib/pages/custom_crs/custom_crs.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class _CustomCrsPageState extends State<CustomCrsPage> {
8080
// Some goeserver changes origin based on zoom level
8181
// and some are not at all (use explicit/implicit null or use [Point(0, 0)])
8282
// @see https://github.com/kartena/Proj4Leaflet/pull/171
83-
origins: [const Point(0, 0)],
83+
origins: const [Point(0, 0)],
8484
// Scale factors (pixels per projection unit, for example pixels/meter) for zoom levels;
8585
// specify either scales or resolutions, not both
8686
scales: null,
@@ -167,7 +167,7 @@ class _CustomCrsPageState extends State<CustomCrsPage> {
167167
format: 'image/jpeg',
168168
baseUrl:
169169
'https://www.gebco.net/data_and_products/gebco_web_services/north_polar_view_wms/mapserv?',
170-
layers: ['gebco_north_polar_view'],
170+
layers: const ['gebco_north_polar_view'],
171171
),
172172
),
173173
),

example/lib/pages/epsg3413_crs.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class _EPSG3413PageState extends State<EPSG3413Page> {
5757
proj4Projection: epsg3413,
5858
resolutions: resolutions,
5959
bounds: epsg3413Bounds,
60-
origins: [const Point(0, 0)],
60+
origins: const [Point(0, 0)],
6161
scales: null,
6262
transformation: null,
6363
);
@@ -67,8 +67,8 @@ class _EPSG3413PageState extends State<EPSG3413Page> {
6767
Widget build(BuildContext context) {
6868
// These circles should have the same pixel radius on the map
6969
final circles = [
70-
CircleMarker(
71-
point: const LatLng(90, 0),
70+
const CircleMarker(
71+
point: LatLng(90, 0),
7272
radius: 20000,
7373
useRadiusInMeter: true,
7474
color: Colors.yellow,
@@ -162,7 +162,7 @@ class _EPSG3413PageState extends State<EPSG3413Page> {
162162
format: 'image/jpeg',
163163
baseUrl:
164164
'https://www.gebco.net/data_and_products/gebco_web_services/north_polar_view_wms/mapserv?',
165-
layers: ['gebco_north_polar_view'],
165+
layers: const ['gebco_north_polar_view'],
166166
),
167167
),
168168
),

example/lib/pages/epsg4326_crs.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class EPSG4326Page extends StatelessWidget {
3434
wmsOptions: WMSTileLayerOptions(
3535
crs: const Epsg4326(),
3636
baseUrl: 'https://ows.mundialis.de/services/service?',
37-
layers: ['TOPO-OSM-WMS'],
37+
layers: const ['TOPO-OSM-WMS'],
3838
),
3939
userAgentPackageName: 'dev.fleaflet.flutter_map.example',
4040
),

example/lib/pages/wms_tile_layer.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class WMSLayerPage extends StatelessWidget {
5454
TileLayer(
5555
wmsOptions: WMSTileLayerOptions(
5656
baseUrl: 'https://{s}.s2maps-tiles.eu/wms/?',
57-
layers: ['s2cloudless-2021_3857'],
57+
layers: const ['s2cloudless-2021_3857'],
5858
),
5959
subdomains: const ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
6060
userAgentPackageName: 'dev.fleaflet.flutter_map.example',

lib/src/geo/crs.dart

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:proj4dart/proj4dart.dart' as proj4;
1111
///
1212
/// The main objective of a CRS is to handle the conversion between surface
1313
/// points of objects of different dimensions. In our case 3D and 2D objects.
14+
@immutable
1415
abstract class Crs {
1516
const Crs();
1617

@@ -26,15 +27,15 @@ abstract class Crs {
2627
try {
2728
final projectedPoint = projection.project(latlng);
2829
final scale = this.scale(zoom);
29-
return transformation.transform(projectedPoint, scale.toDouble());
30-
} catch (e) {
30+
return transformation.transform(projectedPoint, scale);
31+
} catch (_) {
3132
return const Point(0, 0);
3233
}
3334
}
3435

3536
/// Converts a map point to the sphere coordinate (at a certain zoom).
36-
LatLng pointToLatLng(Point point, double zoom) => projection
37-
.unproject(transformation.untransform(point, scale(zoom).toDouble()));
37+
LatLng pointToLatLng(Point point, double zoom) =>
38+
projection.unproject(transformation.untransform(point, scale(zoom)));
3839

3940
/// Zoom to Scale function.
4041
double scale(double zoom) => 256.0 * math.pow(2, zoom);
@@ -48,8 +49,8 @@ abstract class Crs {
4849

4950
final b = projection.bounds!;
5051
final s = scale(zoom);
51-
final min = transformation.transform(b.min, s.toDouble());
52-
final max = transformation.transform(b.max, s.toDouble());
52+
final min = transformation.transform(b.min, s);
53+
final max = transformation.transform(b.max, s);
5354
return Bounds(min, max);
5455
}
5556

@@ -61,6 +62,7 @@ abstract class Crs {
6162
}
6263

6364
// Custom CRS for non geographical maps
65+
@immutable
6466
class CrsSimple extends Crs {
6567
@override
6668
final String code = 'CRS.SIMPLE';
@@ -71,7 +73,7 @@ class CrsSimple extends Crs {
7173
@override
7274
final Transformation transformation;
7375

74-
CrsSimple()
76+
const CrsSimple()
7577
: projection = const _LonLat(),
7678
transformation = const Transformation(1, 0, -1, 0),
7779
super();
@@ -86,6 +88,7 @@ class CrsSimple extends Crs {
8688
(double, double)? get wrapLng => null;
8789
}
8890

91+
@immutable
8992
abstract class Earth extends Crs {
9093
@override
9194
bool get infinite => false;
@@ -100,6 +103,7 @@ abstract class Earth extends Crs {
100103
}
101104

102105
/// The most common CRS used for rendering maps.
106+
@immutable
103107
class Epsg3857 extends Earth {
104108
@override
105109
final String code = 'EPSG:3857';
@@ -123,6 +127,7 @@ class Epsg3857 extends Earth {
123127
}
124128

125129
/// A common CRS among GIS enthusiasts. Uses simple Equirectangular projection.
130+
@immutable
126131
class Epsg4326 extends Earth {
127132
@override
128133
final String code = 'EPSG:4326';
@@ -140,6 +145,7 @@ class Epsg4326 extends Earth {
140145
}
141146

142147
/// Custom CRS
148+
@immutable
143149
class Proj4Crs extends Crs {
144150
@override
145151
final String code;
@@ -163,7 +169,7 @@ class Proj4Crs extends Crs {
163169

164170
final List<double> _scales;
165171

166-
Proj4Crs._({
172+
const Proj4Crs._({
167173
required this.code,
168174
required this.projection,
169175
required this.transformation,
@@ -229,17 +235,16 @@ class Proj4Crs extends Crs {
229235
final scale = this.scale(zoom);
230236
final transformation = _getTransformationByZoom(zoom);
231237

232-
return transformation.transform(projectedPoint, scale.toDouble());
233-
} catch (e) {
238+
return transformation.transform(projectedPoint, scale);
239+
} catch (_) {
234240
return const Point(0, 0);
235241
}
236242
}
237243

238244
/// Converts a map point to the sphere coordinate (at a certain zoom).
239245
@override
240-
LatLng pointToLatLng(Point point, double zoom) =>
241-
projection.unproject(_getTransformationByZoom(zoom)
242-
.untransform(point, scale(zoom).toDouble()));
246+
LatLng pointToLatLng(Point point, double zoom) => projection.unproject(
247+
_getTransformationByZoom(zoom).untransform(point, scale(zoom)));
243248

244249
/// Rescales the bounds to a given zoom value.
245250
@override
@@ -251,8 +256,8 @@ class Proj4Crs extends Crs {
251256

252257
final transformation = _getTransformationByZoom(zoom);
253258

254-
final min = transformation.transform(b.min, s.toDouble());
255-
final max = transformation.transform(b.max, s.toDouble());
259+
final min = transformation.transform(b.min, s);
260+
final max = transformation.transform(b.max, s);
256261
return Bounds(min, max);
257262
}
258263

@@ -267,7 +272,7 @@ class Proj4Crs extends Crs {
267272
final baseScale = _scales[iZoom];
268273
final nextScale = _scales[iZoom + 1];
269274
final scaleDiff = nextScale - baseScale;
270-
final zDiff = (zoom - iZoom);
275+
final zDiff = zoom - iZoom;
271276
return baseScale + scaleDiff * zDiff;
272277
}
273278
}
@@ -319,6 +324,7 @@ class Proj4Crs extends Crs {
319324
}
320325
}
321326

327+
@immutable
322328
abstract class Projection {
323329
const Projection();
324330

@@ -367,6 +373,7 @@ class _LonLat extends Projection {
367373
}
368374
}
369375

376+
@immutable
370377
class SphericalMercator extends Projection {
371378
static const int r = 6378137;
372379
static const double maxLatitude = 85.0511287798;
@@ -402,6 +409,7 @@ class SphericalMercator extends Projection {
402409
}
403410
}
404411

412+
@immutable
405413
class _Proj4Projection extends Projection {
406414
final proj4.Projection epsg4326;
407415

@@ -432,6 +440,7 @@ class _Proj4Projection extends Projection {
432440
}
433441
}
434442

443+
@immutable
435444
class Transformation {
436445
final num a;
437446
final num b;

lib/src/geo/latlng_bounds.dart

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,21 @@ class LatLngBounds {
1313
LatLng corner2,
1414
) : this.fromPoints([corner1, corner2]);
1515

16-
LatLngBounds.fromPoints(List<LatLng> points) : assert(points.isNotEmpty) {
16+
LatLngBounds.fromPoints(List<LatLng> points)
17+
: assert(
18+
points.isNotEmpty,
19+
'LatLngBounds cannot be created with an empty List of LatLng',
20+
) {
1721
double minX = 180;
1822
double maxX = -180;
1923
double minY = 90;
2024
double maxY = -90;
2125

2226
for (final point in points) {
23-
final double x = point.longitude;
24-
final double y = point.latitude;
25-
26-
if (minX > x) {
27-
minX = x;
28-
}
29-
30-
if (minY > y) {
31-
minY = y;
32-
}
33-
34-
if (maxX < x) {
35-
maxX = x;
36-
}
37-
38-
if (maxY < y) {
39-
maxY = y;
40-
}
27+
minX = math.min<double>(minX, point.longitude);
28+
minY = math.min<double>(minY, point.latitude);
29+
maxX = math.max<double>(maxX, point.longitude);
30+
maxY = math.max<double>(maxY, point.latitude);
4131
}
4232

4333
_sw = LatLng(minY, minX);

0 commit comments

Comments
 (0)