Skip to content

Commit a5c99f2

Browse files
authored
Add nuvigator maybeOf (#117)
* Add Nuvigator.maybeOf * Change initialArguments type * Fix example * Add 2.0.0-beta.5 changelog
1 parent e62e0ec commit a5c99f2

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# CHANGELOG
22

3+
## 2.0.0-beta.5
4+
- [BREAKING] Update dart SDK to `>=2.17 < 4.0.0`
5+
- [BREAKING] Split the `Nuvigator.of` nullOk's logic in two methods: `Nuvigator.of` and `Nuvigator.maybeOf`
6+
- [BREAKING] Change Nuvigator's `initialArguments` type to `Map<String, dynamic>?`
7+
38
## 2.0.0-beta.4
49
- [FIX] Fix nullability check when there are no path parameters to be extracted
510

example/lib/samples/screens/home_screen.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class HomeScreen extends StatelessWidget {
3636
onPressed: () {
3737
// final r = NuRouter.of<OldFriendRequestRouter>(context);
3838
// r.toListRequests();
39-
nuvigator?.open<void>(
39+
nuvigator.open<void>(
4040
'exapp://friend-requests?numberOfRequests=10',
4141
);
4242
},
@@ -46,7 +46,7 @@ class HomeScreen extends StatelessWidget {
4646
onPressed: () async {
4747
String? text;
4848

49-
text = await nuvigator?.open<String>(
49+
text = await nuvigator.open<String>(
5050
'exapp://composer/text?initialText=Hello+deep+link%21',
5151
screenType: cupertinoDialogScreenType,
5252
);

lib/src/nuvigator.dart

+31-17
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class _NuvigatorInner<T extends INuRouter> extends Navigator {
7979
_NuvigatorInner({
8080
required this.router,
8181
required String initialDeepLink,
82-
Map<String, Object>? initialArguments,
82+
Map<String, dynamic>? initialArguments,
8383
Key? key,
8484
List<NavigatorObserver> observers = const [],
8585
this.screenType = materialScreenType,
@@ -133,7 +133,7 @@ class _NuvigatorInner<T extends INuRouter> extends Navigator {
133133
class NuvigatorState<T extends INuRouter> extends NavigatorState
134134
with WidgetsBindingObserver {
135135
NuvigatorState get rootNuvigator =>
136-
Nuvigator.of(context, rootNuvigator: true) ?? this;
136+
Nuvigator.maybeOf(context, rootNuvigator: true) ?? this;
137137

138138
List<NuvigatorState> nestedNuvigators = [];
139139

@@ -174,7 +174,7 @@ class NuvigatorState<T extends INuRouter> extends NavigatorState
174174

175175
@override
176176
void initState() {
177-
parent = Nuvigator.of(context, nullOk: true);
177+
parent = Nuvigator.maybeOf(context);
178178
if (isNested) {
179179
parent!.nestedNuvigators.add(this);
180180
}
@@ -549,35 +549,49 @@ class Nuvigator<T extends INuRouter?> extends StatelessWidget {
549549
final List<ObserverBuilder> inheritableObservers;
550550
final List<NavigatorObserver> observers;
551551
final Key? _innerKey;
552-
final Map<String, Object>? initialArguments;
552+
final Map<String, dynamic>? initialArguments;
553553
final ShouldRebuildFn? shouldRebuild;
554554

555-
/// Fetches a [NuvigatorState] from the current BuildContext.
556-
static NuvigatorState<T>? of<T extends INuRouter>(
555+
/// Maybe fetches a [NuvigatorState] from the current BuildContext.
556+
static NuvigatorState<T>? maybeOf<T extends INuRouter>(
557557
BuildContext context, {
558558
bool rootNuvigator = false,
559-
bool nullOk = false,
560559
}) {
561560
if (rootNuvigator) {
562561
return context.findRootAncestorStateOfType<NuvigatorState<T>>();
563562
} else {
564563
final closestNuvigator =
565564
context.findAncestorStateOfType<NuvigatorState<T>>();
566565
if (closestNuvigator != null) return closestNuvigator;
567-
assert(() {
568-
if (!nullOk) {
569-
throw FlutterError(
570-
'Nuvigator operation requested with a context that does not include a Nuvigator.\n'
571-
'The context used to push or pop routes from the Nuvigator must be that of a '
572-
'widget that is a descendant of a Nuvigator widget.'
573-
'Also check if the provided Router [T] type exists withing a the Nuvigator context.');
574-
}
575-
return true;
576-
}());
566+
577567
return null;
578568
}
579569
}
580570

571+
/// Fetches a [NuvigatorState] from the current BuildContext, or throws an
572+
/// error if doesn't find it
573+
static NuvigatorState<T> of<T extends INuRouter>(
574+
BuildContext context, {
575+
bool rootNuvigator = false,
576+
}) {
577+
final nuvigatorState =
578+
Nuvigator.maybeOf<T>(context, rootNuvigator: rootNuvigator);
579+
580+
assert(() {
581+
if (nuvigatorState == null) {
582+
throw FlutterError(
583+
'Nuvigator operation requested with a context that does not include a Nuvigator.\n'
584+
'The context used to push or pop routes from the Nuvigator must be that of a '
585+
'widget that is a descendant of a Nuvigator widget.'
586+
'Also check if the provided Router [T] type exists withing a the Nuvigator context.');
587+
}
588+
589+
return true;
590+
}());
591+
592+
return nuvigatorState!;
593+
}
594+
581595
/// Helper method that allows passing a Nuvigator to a builder function
582596
Nuvigator call(BuildContext context, [Widget? child]) {
583597
return this;

0 commit comments

Comments
 (0)