Skip to content

Conversation

@fzyzcjy
Copy link
Contributor

@fzyzcjy fzyzcjy commented Apr 19, 2023

The code is self-explainable :) Just use firstWhereOrNull to cleanup the code.

List which issues are fixed by this PR. You must list at least one issue.

If you had to change anything in the flutter/tests repo, include a link to the migration guide as per the breaking change policy.

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I signed the CLA.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@fzyzcjy fzyzcjy changed the title Update navigator.dart Tiny cleanup for Navigator code Apr 19, 2023
@flutter-dashboard flutter-dashboard bot added f: routes Navigator, Router, and related APIs. framework flutter/packages/flutter repository. See also f: labels. labels Apr 19, 2023
@fzyzcjy fzyzcjy marked this pull request as ready for review April 19, 2023 07:18
@HansMuller HansMuller requested a review from chunhtai April 21, 2023 21:44
Copy link
Contributor

@chunhtai chunhtai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@chunhtai
Copy link
Contributor

can you rebase off latest tip of tree? it should fix the ci

import 'dart:developer' as developer;
import 'dart:ui' as ui;

import 'package:collection/collection.dart';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we try to avoid depending on other packages in this repo. ideally we should remove all the dependencies on package:collection rather than adding more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good to me. In that case, I don't think this change worth making

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, then what about adding the firstWhereOrNull as a (private) extension method?

extension<T> on List<T> {
  firstWhereOrNull() => ...;
}

By doing so, IMHO the codebase is still a bit neater, since (1) collection package is well-known so people feel familiar when reading firstWhereOrNull and knows what it is (2) the firstWhereOrNull name looks a bit cleaner than .cast().where(... orElse: null) and introduce less cognitive overhead.

It is just like extracting the common parts (repeated 10 times in this file) into an (extension) function.

import 'dart:developer' as developer;
import 'dart:ui' as ui;

import 'package:collection/collection.dart';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good to me. In that case, I don't think this change worth making

@chunhtai
Copy link
Contributor

closing this pr per comment above

@chunhtai chunhtai closed this Apr 21, 2023
@fzyzcjy
Copy link
Contributor Author

fzyzcjy commented Apr 22, 2023

(replied to above)

auto-submit bot pushed a commit that referenced this pull request May 8, 2023
…5628)

Close #125724

> The `navigator.dart` has ~10 repeats of things like:
> 
> ```dart
>     final _RouteEntry? currentRouteEntry = _navigator!._history.cast<_RouteEntry?>().lastWhere(
>       (_RouteEntry? e) => e != null && _RouteEntry.isPresentPredicate(e),
>       orElse: () => null,
>     );
> ```
> 
> while it can be greatly simplified as:
> 
> ```dart
>     final _RouteEntry? currentRouteEntry = _navigator!._history.lastWhereOrNull(_RouteEntry.isPresentPredicate);
> ```
> 
> Thus, it seems that we can beautify the code a little bit.

Same as #125099, but without external dependency

For more detailed explanation why it does not introduce external dependency: #125099 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

f: routes Navigator, Router, and related APIs. framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants