14

The error is when I try to use the ModalRoute.of(context).settings.arguments I get an error. Also I want to save it in a map but can't.

Map data = {};
data = ModalRoute.of(context).settings.arguments; // this is the error;

Error: A value of type 'Object?' can't be assigned to a variable of type 'Map<dynamic, dynamic>'.

  • 'Object' is from 'dart:core'.
  • 'Map' is from 'dart:core'. data = ModalRoute.of(context).settings.arguments;
Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
Mohammad Kaif Ali
  • 151
  • 1
  • 1
  • 3

8 Answers8

39

After passing so many problems, finally, the solution is surprisingly easy.

Map data = {};
data = ModalRoute.of(context).settings.arguments as Map; 
Sawradip Saha
  • 1,151
  • 11
  • 14
14

Try using:

data=ModalRoute.of(context)?.settings?.arguments as Map;

The ? is used for null safety

Art
  • 2,836
  • 4
  • 17
  • 34
noob27
  • 147
  • 1
  • 2
3

after lot of searching this coda works

Map data = {};
data = ModalRoute.of(context)?.settings.arguments as Map;

2

You can do that with the below code:

Map <String, Object>data = {}; // as the arguments passed from 
data = {"dataKey":ModalRoute.of(context).settings.arguments};

And later, access your arguments by calling

data["dataKey"] // it will return the arguments

Gourango Sutradhar
  • 1,461
  • 10
  • 16
1

I was facing a similar issue and I think this solution will help you

Map data = ModalRoute.of(context)!.settings.arguments as Map;
1

Put the below code inside of your StatefulWidget class

Map userdata = {};
userdata = ModalRoute.of(context)!.settings.arguments as Map;
print(userdata);

Your output looks like something this

flutter: {firstName: Dhaval, lastname: Gevariya}
Dhaval Gevariya
  • 870
  • 1
  • 12
  • 16
0
  1. Define the argument Like :

    class ProductDetailsArguments { final Product product;

    ProductDetailsArguments({required this.product});

    }

  2. extracts the arguments Like

    final args = ModalRoute.of(context)!.settings.arguments as ProductDetailsArguments;

TuGordoBello
  • 4,350
  • 9
  • 52
  • 78
0

To solve the issue, I did :

Map <String, dynamic> data = {}; data = ModalRoute.of(context).settings.arguments as Map<String, dynamic>;

Confiance
  • 706
  • 5
  • 7