Allen Wong
Allen Wong

Reputation: 1433

How to define Navigator.pop() response type in Child widget

Parent Widget

String pincode = await Navigators.root.currentState.push(
  MaterialPageRoute<String>(
    builder: (context) => ChildWidget(),
  ),
);

Child Widget

Navigators.root.currentState.pop(pincode);

My concerns:

  1. If someone change the Child .pop response to bool. IDE will never know and show warnings in Parent Widget.
  2. If you don't look into the Child widget implementation, you will not know what type does it return.

current solution

class ChildResult {
  final String pincode;
  ChildResult({ this.pincode });
}

class Child extends HookWidget {
    ...
    Navigators.root.currentState.pop(ChildResult(pincode: pincode));
    ...
}

My current solution is to create a [Widget]Result class, and told my coworker to remain same practice for all widget, so at least I can preview what's value name / type in IDE.

Is there any other solution?

Upvotes: 0

Views: 398

Answers (1)

Sam Smets
Sam Smets

Reputation: 240

As far as I know it is impossible at the moment to define a type for the pop function on the Navigator. When you call the pop the type is dynamic because it is unaware of which page (widget) it will be popped to in the stack.

Upvotes: 1

Related Questions