Skip to content

Commit 2184779

Browse files
authored
Merge pull request flutter#342 from nikitadol/master
fix types, use Pattern
2 parents 080d387 + 2015eee commit 2184779

File tree

7 files changed

+40
-34
lines changed

7 files changed

+40
-34
lines changed

package/lib/src/beam_guard.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class BeamGuard {
3838
/// For example, `RegExp('/books/')` will match '/books/1', '/books/2/genres', etc.
3939
/// but will not match '/books'. To match '/books' and everything after it,
4040
/// use `RegExp('/books')`
41-
final List<dynamic> pathBlueprints;
41+
final List<Pattern> pathBlueprints;
4242

4343
/// What check should be performed on a given [location],
4444
/// the one to which beaming has been requested.
@@ -83,7 +83,7 @@ class BeamGuard {
8383
/// Else, the path (i.e. the pre-query substring) of the location's uri
8484
/// must be equal to the pathBlueprint.
8585
bool _hasMatch(BeamLocation location) {
86-
for (var pathBlueprint in pathBlueprints) {
86+
for (final pathBlueprint in pathBlueprints) {
8787
if (pathBlueprint is String) {
8888
final asteriskIndex = pathBlueprint.indexOf('*');
8989
if (asteriskIndex != -1) {

package/lib/src/beam_location.dart

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ abstract class BeamLocation<T extends BeamState> extends ChangeNotifier {
2323
/// Upon beaming, it will be populated by all necessary attributes.
2424
/// See [BeamState].
2525
T get state => _state;
26+
2627
set state(T state) => _state = state..configure();
2728

2829
/// How to create state from generic [BeamState], that is produced
@@ -82,7 +83,7 @@ abstract class BeamLocation<T extends BeamState> extends ChangeNotifier {
8283
/// whether there is a browser.
8384
///
8485
/// For example: '/books/:id' or using regex `RegExp('/test/(?<test>[a-z]+){0,1}')`
85-
List<dynamic> get pathBlueprints;
86+
List<Pattern> get pathBlueprints;
8687

8788
/// Creates and returns the list of pages to be built by the [Navigator]
8889
/// when this [BeamLocation] is beamed to or internally inferred.
@@ -145,7 +146,7 @@ class SimpleBeamLocation extends BeamLocation {
145146
}) : super(state);
146147

147148
/// Map of all routes this location handles.
148-
Map<dynamic, dynamic Function(BuildContext, BeamState)> routes;
149+
Map<Pattern, dynamic Function(BuildContext, BeamState)> routes;
149150

150151
/// A wrapper used as [BeamLocation.builder].
151152
Widget Function(BuildContext context, Widget navigator)? navBuilder;
@@ -165,12 +166,12 @@ class SimpleBeamLocation extends BeamLocation {
165166
}
166167

167168
@override
168-
List<dynamic> get pathBlueprints => routes.keys.toList();
169+
List<Pattern> get pathBlueprints => routes.keys.toList();
169170

170171
@override
171172
List<BeamPage> buildPages(BuildContext context, BeamState state) {
172-
var filteredRoutes = chooseRoutes(state, routes.keys);
173-
final activeRoutes = Map.from(routes)
173+
final filteredRoutes = chooseRoutes(state, routes.keys);
174+
final activeRoutes = Map.of(routes)
174175
..removeWhere((key, value) => !filteredRoutes.containsKey(key));
175176
final sortedRoutes = activeRoutes.keys.toList()
176177
..sort((a, b) => _compareKeys(a, b));
@@ -191,13 +192,13 @@ class SimpleBeamLocation extends BeamLocation {
191192
///
192193
/// If none of the routes _matches_ [state.uri], nothing will be selected
193194
/// and [BeamerDelegate] will declare that the location is [NotFound].
194-
static Map<dynamic, String> chooseRoutes(
195-
BeamState state, Iterable<dynamic> routes) {
196-
var matched = <dynamic, String>{};
195+
static Map<Pattern, String> chooseRoutes(
196+
BeamState state, Iterable<Pattern> routes) {
197+
final matched = <Pattern, String>{};
197198
bool overrideNotFound = false;
198-
for (var route in routes) {
199+
for (final route in routes) {
199200
if (route is String) {
200-
final uriPathSegments = List.from(state.uri.pathSegments);
201+
final uriPathSegments = state.uri.pathSegments.toList();
201202
final routePathSegments = Uri.parse(route).pathSegments;
202203

203204
if (uriPathSegments.length < routePathSegments.length) {

package/lib/src/beam_state.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:convert';
2+
13
import 'package:flutter/foundation.dart';
24
import 'package:flutter/widgets.dart';
35

@@ -13,7 +15,10 @@ class BeamState {
1315
this.pathParameters = const <String, String>{},
1416
this.queryParameters = const <String, String>{},
1517
this.data = const <String, dynamic>{},
16-
}) {
18+
}) : assert(() {
19+
json.encode(data);
20+
return true;
21+
}()) {
1722
configure();
1823
}
1924

@@ -126,7 +131,7 @@ class BeamState {
126131
path: '/' + pathBlueprintSegments.join('/'),
127132
queryParameters: queryParameters.isEmpty ? null : queryParameters,
128133
);
129-
final pathSegments = List<String>.from(pathBlueprintSegments);
134+
final pathSegments = pathBlueprintSegments.toList();
130135
for (int i = 0; i < pathSegments.length; i++) {
131136
if (pathSegments[i].isNotEmpty && pathSegments[i][0] == ':') {
132137
final key = pathSegments[i].substring(1);

package/lib/src/beamer_delegate.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ class BeamerDelegate<T extends BeamState> extends RouterDelegate<BeamState>
684684
BuildContext context,
685685
BeamLocation location,
686686
) {
687-
for (var guard in guards + location.guards) {
687+
for (final guard in guards + location.guards) {
688688
if (guard.shouldGuard(location) && !guard.check(context, location)) {
689689
guard.onCheckFailed?.call(context, location);
690690
return guard;

package/lib/src/beamer_location_builder.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ class SimpleLocationBuilder {
2828
SimpleLocationBuilder({required this.routes, this.builder});
2929

3030
/// List of all routes this builder handles.
31-
final Map<dynamic, dynamic Function(BuildContext, BeamState)> routes;
31+
final Map<Pattern, dynamic Function(BuildContext, BeamState)> routes;
3232

3333
/// Used as a [BeamLocation.builder].
3434
Widget Function(BuildContext context, Widget navigator)? builder;
3535

3636
BeamLocation call(BeamState state) {
37-
var matched = SimpleBeamLocation.chooseRoutes(state, routes.keys);
37+
final matched = SimpleBeamLocation.chooseRoutes(state, routes.keys);
3838
if (matched.isNotEmpty) {
3939
return SimpleBeamLocation(
4040
state: state,

package/lib/src/utils.dart

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ abstract class Utils {
1515
List<BeamLocation> beamLocations, {
1616
Map<String, dynamic> data = const <String, dynamic>{},
1717
}) {
18-
for (var beamLocation in beamLocations) {
18+
for (final beamLocation in beamLocations) {
1919
if (canBeamLocationHandleUri(beamLocation, uri)) {
2020
return beamLocation
2121
..state = beamLocation.createState(
@@ -34,12 +34,12 @@ abstract class Utils {
3434
///
3535
/// Used in [BeamLocation.canHandle] and [chooseBeamLocation].
3636
static bool canBeamLocationHandleUri(BeamLocation beamLocation, Uri uri) {
37-
for (var pathBlueprint in beamLocation.pathBlueprints) {
37+
for (final pathBlueprint in beamLocation.pathBlueprints) {
3838
if (pathBlueprint is String) {
3939
if (pathBlueprint == uri.path || pathBlueprint == '/*') {
4040
return true;
4141
}
42-
final uriPathSegments = List.from(uri.pathSegments);
42+
final uriPathSegments = uri.pathSegments.toList();
4343
if (uriPathSegments.length > 1 && uriPathSegments.last == '') {
4444
uriPathSegments.removeLast();
4545
}
@@ -82,7 +82,7 @@ abstract class Utils {
8282
}) {
8383
if (beamLocation != null) {
8484
// TODO: abstract this and reuse in canBeamLocationHandleUri
85-
for (var pathBlueprint in beamLocation.pathBlueprints) {
85+
for (final pathBlueprint in beamLocation.pathBlueprints) {
8686
if (pathBlueprint is String) {
8787
if (pathBlueprint == uri.path || pathBlueprint == '/*') {
8888
BeamState(
@@ -91,14 +91,14 @@ abstract class Utils {
9191
data: data,
9292
);
9393
}
94-
final uriPathSegments = List.from(uri.pathSegments);
94+
final uriPathSegments = uri.pathSegments.toList();
9595
if (uriPathSegments.length > 1 && uriPathSegments.last == '') {
9696
uriPathSegments.removeLast();
9797
}
9898
final beamLocationPathBlueprintSegments =
9999
Uri.parse(pathBlueprint).pathSegments;
100100
var pathSegments = <String>[];
101-
var pathParameters = <String, String>{};
101+
final pathParameters = <String, String>{};
102102
if (uriPathSegments.length >
103103
beamLocationPathBlueprintSegments.length &&
104104
!beamLocationPathBlueprintSegments.contains('*')) {
@@ -107,7 +107,7 @@ abstract class Utils {
107107
var checksPassed = true;
108108
for (int i = 0; i < uriPathSegments.length; i++) {
109109
if (beamLocationPathBlueprintSegments[i] == '*') {
110-
pathSegments = List<String>.from(uriPathSegments);
110+
pathSegments = uriPathSegments.toList();
111111
checksPassed = true;
112112
break;
113113
}
@@ -133,12 +133,12 @@ abstract class Utils {
133133
}
134134
} else {
135135
final regexp = tryCastToRegExp(pathBlueprint);
136-
var pathParameters = <String, String>{};
136+
final pathParameters = <String, String>{};
137137
final url = uri.toString();
138138

139139
if (regexp.hasMatch(url)) {
140140
regexp.allMatches(url).forEach((match) {
141-
for (String groupName in match.groupNames) {
141+
for (final groupName in match.groupNames) {
142142
pathParameters[groupName] = match.namedGroup(groupName) ?? '';
143143
}
144144
});
@@ -159,10 +159,10 @@ abstract class Utils {
159159
);
160160
}
161161

162-
static bool urisMatch(dynamic blueprint, Uri exact) {
162+
static bool urisMatch(Pattern blueprint, Uri exact) {
163163
if (blueprint is String) {
164-
blueprint = Uri.parse(blueprint);
165-
final blueprintSegments = blueprint.pathSegments;
164+
final uriBlueprint = Uri.parse(blueprint);
165+
final blueprintSegments = uriBlueprint.pathSegments;
166166
final exactSegment = exact.pathSegments;
167167
if (blueprintSegments.length != exactSegment.length) {
168168
return false;
@@ -177,14 +177,14 @@ abstract class Utils {
177177
}
178178
return true;
179179
} else {
180-
blueprint = tryCastToRegExp(blueprint);
181-
return blueprint.hasMatch(exact.toString());
180+
final regExpBlueprint = tryCastToRegExp(blueprint);
181+
return regExpBlueprint.hasMatch(exact.toString());
182182
}
183183
}
184184

185185
/// Wraps the casting of pathBlueprint to RegExp inside a try-catch
186186
/// and throws a nice FlutterError.
187-
static RegExp tryCastToRegExp(dynamic pathBlueprint) {
187+
static RegExp tryCastToRegExp(Pattern pathBlueprint) {
188188
try {
189189
return pathBlueprint as RegExp;
190190
} on TypeError catch (_) {

package/test/test_locations.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class RegExpLocation extends BeamLocation {
133133
RegExpLocation([BeamState? state]) : super(state);
134134

135135
@override
136-
List get pathBlueprints => [RegExp('/reg')];
136+
List<Pattern> get pathBlueprints => [RegExp('/reg')];
137137

138138
@override
139139
List<BeamPage> buildPages(BuildContext context, BeamState state) => [
@@ -148,7 +148,7 @@ class AsteriskLocation extends BeamLocation {
148148
AsteriskLocation([BeamState? state]) : super(state);
149149

150150
@override
151-
List get pathBlueprints => ['/anything/*'];
151+
List<Pattern> get pathBlueprints => ['/anything/*'];
152152

153153
@override
154154
List<BeamPage> buildPages(BuildContext context, BeamState state) => [

0 commit comments

Comments
 (0)