Get passed arguments from Navigator in Widget's state's initState

 The accepted should be didChangeDependencies.

late Object args; 

@override
void didChangeDependencies() {
  args = ModalRoute.of(context).settings.argument
  super.didChangeDependencies();
}

@override
Widget build(BuildContext context) {
   /// use args here 
}

It's mentioned in the docs

This method is also called immediately after initState. It is safe to call BuildContext.dependOnInheritedWidgetOfExactType from this method.

It's also mentioned in your error code

initialization based on inherited widgets can be placed in the didChangeDependencies is called after initState and whenever the dependencies change thereafter.

Post a Comment

Previous Post Next Post

Subscribe Us


Get tutorials, Flutter news and other exclusive content delivered to your inbox. Join 1000+ growth-oriented Flutter developers subscribed to the newsletter

100% value, 0% spam. Unsubscribe anytime