I wrote a model like this:
List<School> schools = [
School(city: "ISTANBUL", name: "Exam1"),
School(city: "ISTANBUL", name: "Name2"),
School(city: "ISTANBUL", name: "Name3")
];
List allSchools() {
return schools;
}
class School {
String city;
String name;
School({required this.city, required this.name});
}
I want to be able to use city
and name
values in the list in Flutter.
Flutter codes:
TextFieldSearch(
decoration: InputDecoration(
hintText: "Select School",
prefixIcon: const Icon(
Icons.search,
color: Colors.black45,
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide.none,
),
filled: true,
fillColor: const Color.fromARGB(255, 229, 229, 229),
),
initialList: allSchools(),
label: "",
controller: _schoolController,
),
How can I use model and list together? I hope you understand my point. Thanks in advance for your help.
Error
CodePudding user response:
The package you used only support one string, there is a workaround for that which is convert your schools
list to a newlist
like this:
List<String> newList = schools.map((e) => '${e.city}-${e.name}').toList();
and use this list to build your TextFieldSearch
.
For cleaner code you can separate this widget like this:
Widget buildSearchField(List<School> schools) {
List<String> newList = schools.map((e) => '${e.city}-${e.name}').toList();
return TextFieldSearch(
decoration: InputDecoration(
hintText: "Select School",
prefixIcon: const Icon(
Icons.search,
color: Colors.black45,
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide.none,
),
filled: true,
fillColor: const Color.fromARGB(255, 229, 229, 229),
),
initialList: newList,
label: "",
controller: _schoolController,
);
}
and use it like this:
buildSearchField(allSchools()),