简体   繁体   中英

how to use string as function in dart?

I am stuck in a situation where i need double doller function like in php. like:

var citis = [];

i want to use:

citis = ServiceList.mycity();

instead:

if(mycity = 'Delhi') { citis = ServiceList.Delhi();  } else
if(mycity = 'Mumbai') { citis = ServiceList.Mumbai();  }

i am sure that is possible but how, can anyone help?? Thanks in advance.

If it were JavaScript, you could have the string: "ServiceList." + mycity + "()" "ServiceList." + mycity + "()" and then executed it using eval function.

Use the Dart Eval package to accomplish the same thing in Dart. Take the ServiceList.mycity() function as a string and replace mycity with whichever city you want. Then, evaluate it using the package.

One thing you can try is to put all the cities and locations in an Key/Value Array in ServiceList and the function ServiceList.cities($city).eg ServiceList.cities('Mumbai') or ServiceList.cities('Delhi')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM