Skip to content

Commit

Permalink
Add nuvigator maybeOf (#117)
Browse files Browse the repository at this point in the history
* Add Nuvigator.maybeOf

* Change initialArguments type

* Fix example

* Add 2.0.0-beta.5 changelog
  • Loading branch information
raapperez authored Sep 13, 2024
1 parent e62e0ec commit a5c99f2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 2.0.0-beta.5
- [BREAKING] Update dart SDK to `>=2.17 < 4.0.0`
- [BREAKING] Split the `Nuvigator.of` nullOk's logic in two methods: `Nuvigator.of` and `Nuvigator.maybeOf`
- [BREAKING] Change Nuvigator's `initialArguments` type to `Map<String, dynamic>?`

## 2.0.0-beta.4
- [FIX] Fix nullability check when there are no path parameters to be extracted

Expand Down
4 changes: 2 additions & 2 deletions example/lib/samples/screens/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class HomeScreen extends StatelessWidget {
onPressed: () {
// final r = NuRouter.of<OldFriendRequestRouter>(context);
// r.toListRequests();
nuvigator?.open<void>(
nuvigator.open<void>(
'exapp://friend-requests?numberOfRequests=10',
);
},
Expand All @@ -46,7 +46,7 @@ class HomeScreen extends StatelessWidget {
onPressed: () async {
String? text;

text = await nuvigator?.open<String>(
text = await nuvigator.open<String>(
'exapp://composer/text?initialText=Hello+deep+link%21',
screenType: cupertinoDialogScreenType,
);
Expand Down
48 changes: 31 additions & 17 deletions lib/src/nuvigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class _NuvigatorInner<T extends INuRouter> extends Navigator {
_NuvigatorInner({
required this.router,
required String initialDeepLink,
Map<String, Object>? initialArguments,
Map<String, dynamic>? initialArguments,
Key? key,
List<NavigatorObserver> observers = const [],
this.screenType = materialScreenType,
Expand Down Expand Up @@ -133,7 +133,7 @@ class _NuvigatorInner<T extends INuRouter> extends Navigator {
class NuvigatorState<T extends INuRouter> extends NavigatorState
with WidgetsBindingObserver {
NuvigatorState get rootNuvigator =>
Nuvigator.of(context, rootNuvigator: true) ?? this;
Nuvigator.maybeOf(context, rootNuvigator: true) ?? this;

List<NuvigatorState> nestedNuvigators = [];

Expand Down Expand Up @@ -174,7 +174,7 @@ class NuvigatorState<T extends INuRouter> extends NavigatorState

@override
void initState() {
parent = Nuvigator.of(context, nullOk: true);
parent = Nuvigator.maybeOf(context);
if (isNested) {
parent!.nestedNuvigators.add(this);
}
Expand Down Expand Up @@ -549,35 +549,49 @@ class Nuvigator<T extends INuRouter?> extends StatelessWidget {
final List<ObserverBuilder> inheritableObservers;
final List<NavigatorObserver> observers;
final Key? _innerKey;
final Map<String, Object>? initialArguments;
final Map<String, dynamic>? initialArguments;
final ShouldRebuildFn? shouldRebuild;

/// Fetches a [NuvigatorState] from the current BuildContext.
static NuvigatorState<T>? of<T extends INuRouter>(
/// Maybe fetches a [NuvigatorState] from the current BuildContext.
static NuvigatorState<T>? maybeOf<T extends INuRouter>(
BuildContext context, {
bool rootNuvigator = false,
bool nullOk = false,
}) {
if (rootNuvigator) {
return context.findRootAncestorStateOfType<NuvigatorState<T>>();
} else {
final closestNuvigator =
context.findAncestorStateOfType<NuvigatorState<T>>();
if (closestNuvigator != null) return closestNuvigator;
assert(() {
if (!nullOk) {
throw FlutterError(
'Nuvigator operation requested with a context that does not include a Nuvigator.\n'
'The context used to push or pop routes from the Nuvigator must be that of a '
'widget that is a descendant of a Nuvigator widget.'
'Also check if the provided Router [T] type exists withing a the Nuvigator context.');
}
return true;
}());

return null;
}
}

/// Fetches a [NuvigatorState] from the current BuildContext, or throws an
/// error if doesn't find it
static NuvigatorState<T> of<T extends INuRouter>(
BuildContext context, {
bool rootNuvigator = false,
}) {
final nuvigatorState =
Nuvigator.maybeOf<T>(context, rootNuvigator: rootNuvigator);

assert(() {
if (nuvigatorState == null) {
throw FlutterError(
'Nuvigator operation requested with a context that does not include a Nuvigator.\n'
'The context used to push or pop routes from the Nuvigator must be that of a '
'widget that is a descendant of a Nuvigator widget.'
'Also check if the provided Router [T] type exists withing a the Nuvigator context.');
}

return true;
}());

return nuvigatorState!;
}

/// Helper method that allows passing a Nuvigator to a builder function
Nuvigator call(BuildContext context, [Widget? child]) {
return this;
Expand Down

0 comments on commit a5c99f2

Please sign in to comment.