Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when using ModalRoute to retrieve arguments

Tags:

flutter

dart

I am getting null every time I call modalroute to retrieve arguments from a named route.

This is the code snippet:

Navigator.pushNamed(
           context, 
           VerifyCode.routeName, 
           arguments: {'code': randValue,},
           );

This is where I retrieve the argument:

Widget build(BuildContext context) 
{
   //Get the dialog provider to display information
   alertViewModel = Provider.of<AlertViewModel>(context);

   //Retrieve the route argument
   final routeArgs =
           ModalRoute.of(context).settings.arguments as Map<String, int>;

    final securityCode = routeArgs['code']; <-- Error happens here

    print('Code =' + securityCode.toString());
   return Container()

}

I should be getting a integer from the map but instead I am getting the error:

 ════════ Exception Caught By widgets library ═══════════════════════════════════
The following NoSuchMethodError was thrown building VerifyCode(dirty, dependencies: [InheritedProvider<AlertViewModel>, _ModalScopeStatus], state: _VerifyCodeState#98992):
The method '[]' was called on null.
Receiver: null
Tried calling: []("code")
When the exception was thrown, this was the stack
#0      Object.noSuchMethod  (dart:core-patch/object_patch.dart:50:5)
#1      _VerifyCodeState.build package:accidentapp/screens/verifycode.dart:42
#2      StatefulElement.build 
package:flutter/…/widgets/framework.dart:4012
#3      ComponentElement.performRebuild package:flutter/…/widgets/framework.dart:3924
#4      Element.rebuild
package:flutter/…/widgets/framework.dart:3721 ═══════════════════════════════════════════════════════════════════

The routeArgs is always null. I have tried using a class to pass the value instead of the map but I am getting the same error. Not sure what I am doing wrong.

like image 915
WXM1967 Avatar asked Jun 21 '26 21:06

WXM1967


1 Answers

Pass settings to MaterialPageRoute

Route<dynamic> generateRoute(RouteSettings settings) {
return MaterialPageRoute(
  builder: (BuildContext context) {
    //some custom code
    return Page();
  },
  settings: settings
);

}

like image 136
Fateme Afshari Avatar answered Jun 23 '26 13:06

Fateme Afshari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!