Home > Net >  Call a class from a string flutter
Call a class from a string flutter

Time:01-05

I have several cities, and when I click on on a specific city I want that the next page shows the model from that city. As each city contains alot of models, I have a seperate class for each models in the city.

List<City> cities = City.fetchAll();
for (i = 0; i < cities.length; i  ) {
  if (cityID == cities[i].id) {
    // returns 'Paris' when paris is clicked on 
    String city = cities[i].name;
    
    }
}
// Then I want to call the class TestParis and fetch the the list from that model: 
final test = TestParis.fetchAll();
// or it could be another city:
final test = TestMadrid.fetchAll();
// 
final test = 'Test${city}'.toClass.fetchAll(); 

So basically convert string to initate a class. How can I do that?

CodePudding user response:

Not possible in Flutter because reflactions not available in flutter ... in dart only there is the mirror package ... but here you can only build a map with constructor functions something like that

final test = {
'TestParis' : TestParis.fetchAll
}['Test${city}'].call() ; 
  •  Tags:  
  • Related