0

After passing data via connectRequest I get a string of values

final connectRequest = ModalRoute.of(context)!.settings.arguments ;
   print(connectRequest);

this is after i printed out connectRequest

{serialNumber: HKGHGH, modelName: GW020H, ipAddr: 192.168.1.1}

How can I call the elements in this list ?

pmatatias
  • 3,491
  • 3
  • 10
  • 30
natt
  • 11
  • 5
  • Please make an effort to describe the problem in your question title. Your pattern of "Flutter #X: How can I achieve this?" is completely generic and prevents other people with the same problem from searching for your question. Additionally, people who might know the answer to your question might ignore it completely. Please see https://stackoverflow.com/help/how-to-ask – jamesdlin Oct 05 '22 at 03:05
  • What does it mean to "call the elements"? – Randal Schwartz Oct 05 '22 at 03:12
  • I want to get the values ​​in the list like 'serialNumber' , 'modelName' and 'ipAddr' – natt Oct 05 '22 at 03:15

1 Answers1

0

Its a Map you can access element like:

print(connectRequest['serialNumber']);

output:

HKGHGH

Map is key : values which you can call key to get value you Can follow this question to read more

mohammad esmaili
  • 1,614
  • 2
  • 4
  • 17
  • as soon as i do this i get an error which is "The method '[]' can't be unconditionally invoked because the receiver can be 'null'. Try making the call conditional (using '?.') or adding a null check to the target ('!')." I also tried adding the ! and ?. however I get another error "The operator '[]' isn't defined for the type 'Object'. Try defining the operator '[]'." – natt Oct 05 '22 at 03:40
  • Can you attach what you are passing to `ModalRoute.of(context)!.settings.arguments`? – mohammad esmaili Oct 05 '22 at 03:48
  • Future connectDevice(Data data) async { var connectRequest = { 'serialNumber': data.serialNumber, 'modelName': data.modelName, 'ipAddr': data.ipAddr, }; print('------------$connectRequest'); var apiEndpoint = API.shared; var res = await apiEndpoint.connectDevice(connectRequest); print(res); Navigator.push(context, MaterialPageRoute(builder: (context) => const DeviceInformation(), settings: RouteSettings(arguments: connectRequest), )); } – natt Oct 05 '22 at 03:51
  • Well, use `final Map connectRequest = ModalRoute.of(context).settings.arguments as Map;` and try what happens – mohammad esmaili Oct 05 '22 at 03:55
  • If didnt help, change `var` into `Map` in where you are passing to new page – mohammad esmaili Oct 05 '22 at 03:58
  • Glad to hear that, if my answer helps, vote to help others too – mohammad esmaili Oct 05 '22 at 04:10