@@ -38,14 +38,14 @@ class GoRouterDelegate extends RouterDelegate<Uri>
38
38
required this .debugLogDiagnostics,
39
39
required this .routerNeglect,
40
40
this .restorationScopeId,
41
- }) {
42
- // check top-level route paths are valid
43
- for (final GoRoute route in routes) {
44
- if ( ! route.path.startsWith ('/' )) {
45
- throw Exception ( 'top-level path must start with "/": ${route .path }' );
46
- }
47
- }
48
-
41
+ }) : assert (() {
42
+ // check top-level route paths are valid
43
+ for (final GoRoute route in routes) {
44
+ assert ( route.path.startsWith ('/' ),
45
+ 'top-level path must start with "/": ${route .path }' );
46
+ }
47
+ return true ;
48
+ }()) {
49
49
// cache the set of named routes for fast lookup
50
50
_cacheNamedRoutes (routes, '' , _namedMatches);
51
51
@@ -110,10 +110,8 @@ class GoRouterDelegate extends RouterDelegate<Uri>
110
110
111
111
if (route.name != null ) {
112
112
final String name = route.name! .toLowerCase ();
113
- if (namedFullpaths.containsKey (name)) {
114
- throw Exception ('duplication fullpaths for name "$name ":'
115
- '${namedFullpaths [name ]!.fullpath }, $fullpath ' );
116
- }
113
+ assert (! namedFullpaths.containsKey (name),
114
+ 'duplication fullpaths for name "$name ":${namedFullpaths [name ]!.fullpath }, $fullpath ' );
117
115
118
116
// we only have a partial match until we have a location;
119
117
// we're really only caching the route and fullpath at this point
@@ -154,12 +152,9 @@ class GoRouterDelegate extends RouterDelegate<Uri>
154
152
params: params,
155
153
queryParams: queryParams,
156
154
);
157
- if (match == null ) {
158
- throw Exception ('unknown route name: $name ' );
159
- }
160
-
161
- assert (identical (match.queryParams, queryParams));
162
- return _addQueryParams (match.subloc, queryParams);
155
+ assert (match != null , 'unknown route name: $name ' );
156
+ assert (identical (match! .queryParams, queryParams));
157
+ return _addQueryParams (match! .subloc, queryParams);
163
158
}
164
159
165
160
/// Navigate to the given location.
@@ -179,12 +174,8 @@ class GoRouterDelegate extends RouterDelegate<Uri>
179
174
/// Pop the top page off the GoRouter's page stack.
180
175
void pop () {
181
176
_matches.remove (_matches.last);
182
- if (_matches.isEmpty) {
183
- throw Exception (
184
- 'have popped the last page off of the stack; '
185
- 'there are no pages left to show' ,
186
- );
187
- }
177
+ assert (_matches.isNotEmpty,
178
+ 'have popped the last page off of the stack; there are no pages left to show' );
188
179
notifyListeners ();
189
180
}
190
181
@@ -301,29 +292,28 @@ class GoRouterDelegate extends RouterDelegate<Uri>
301
292
return false ;
302
293
}
303
294
304
- if (Uri .tryParse (redir) == null ) {
305
- throw Exception ('invalid redirect: $redir ' );
306
- }
307
-
308
- if (redirects.contains (redir)) {
309
- redirects.add (redir);
310
- final String msg =
311
- 'redirect loop detected: ${redirects .join (' => ' )}' ;
312
- throw Exception (msg);
313
- }
295
+ assert (Uri .tryParse (redir) != null , 'invalid redirect: $redir ' );
296
+
297
+ assert (
298
+ ! redirects.contains (redir),
299
+ 'redirect loop detected: ${<String >[
300
+ ...redirects ,
301
+ redir
302
+ ].join (' => ' )}' );
303
+ assert (
304
+ redirects.length < redirectLimit,
305
+ 'too many redirects: ${<String >[
306
+ ...redirects ,
307
+ redir
308
+ ].join (' => ' )}' );
314
309
315
310
redirects.add (redir);
316
- if (redirects.length - 1 > redirectLimit) {
317
- final String msg = 'too many redirects: ${redirects .join (' => ' )}' ;
318
- throw Exception (msg);
319
- }
320
-
321
311
log.info ('redirecting to $redir ' );
322
312
return true ;
323
313
}
324
314
325
315
// keep looping till we're done redirecting
326
- for (;; ) {
316
+ while ( true ) {
327
317
final String loc = redirects.last;
328
318
329
319
// check for top-level redirect
@@ -449,23 +439,20 @@ class GoRouterDelegate extends RouterDelegate<Uri>
449
439
extra: extra,
450
440
).toList ();
451
441
452
- if (matchStacks.isEmpty) {
453
- throw Exception ('no routes for location: $location ' );
454
- }
442
+ assert (matchStacks.isNotEmpty, 'no routes for location: $location ' );
443
+ assert (() {
444
+ if (matchStacks.length > 1 ) {
445
+ final StringBuffer sb = StringBuffer ()
446
+ ..writeln ('too many routes for location: $location ' );
455
447
456
- if (matchStacks.length > 1 ) {
457
- final StringBuffer sb = StringBuffer ()
458
- ..writeln ('too many routes for location: $location ' );
448
+ for (final List <GoRouteMatch > stack in matchStacks) {
449
+ sb.writeln (
450
+ '\t ${stack .map ((GoRouteMatch m ) => m .route .path ).join (' => ' )}' );
451
+ }
459
452
460
- for (final List <GoRouteMatch > stack in matchStacks) {
461
- sb.writeln (
462
- '\t ${stack .map ((GoRouteMatch m ) => m .route .path ).join (' => ' )}' );
453
+ assert (false , sb.toString ());
463
454
}
464
455
465
- throw Exception (sb.toString ());
466
- }
467
-
468
- if (kDebugMode) {
469
456
assert (matchStacks.length == 1 );
470
457
final GoRouteMatch match = matchStacks.first.last;
471
458
final String loc1 = _addQueryParams (match.subloc, match.queryParams);
@@ -475,7 +462,8 @@ class GoRouterDelegate extends RouterDelegate<Uri>
475
462
// NOTE: match the lower case, since subloc is canonicalized to match the
476
463
// path case whereas the location can be any case
477
464
assert (loc1.toLowerCase () == loc2.toLowerCase (), '$loc1 != $loc2 ' );
478
- }
465
+ return true ;
466
+ }());
479
467
480
468
return matchStacks.first;
481
469
}
0 commit comments