I have created an auto complete drop down field which shows data of api and when i search particular data i have saved data into sharedpreference also so that i can show few recently searched list so to achieve that i checked a condition saying if textformcontroller value is empty meaning nothing is types in textform field show recently search drop down and if someone type something in textform field then soo filtred data which come from api.
This is my code
optionsViewBuilder: (BuildContext context,
AutocompleteOnSelected<Result1> onSelected,
Iterable<Result1> options) {
if (fieldTextEditingController.value.text == "") {
return Align(
alignment: Alignment.topLeft,
child: Material(
child: Container(
width: 0.78.w(context),
color: Colors.white,
child: ListView.builder(
padding: const EdgeInsets.all(10.0),
itemCount: titles.length,
itemBuilder:
(BuildContext context, int index) {
final Result1 option =
options.elementAt(index);
return GestureDetector(
onTap: () {
onSelected(option);
},
child: ListTile(
title: Text(titles[index],
style: const TextStyle(
color: Colors.black87)),
trailing: InkWell(
onTap: () {
titles.removeAt(index);
},
child: Text("X")),
),
);
},
),
),
),
);
} else {
return Align(
alignment: Alignment.topLeft,
child: Material(
child: Container(
width: 0.78.w(context),
color: Colors.white,
child: ListView.builder(
padding: const EdgeInsets.all(10.0),
itemCount: options.length,
itemBuilder:
(BuildContext context, int index) {
final Result1 option =
options.elementAt(index);
return GestureDetector(
onTap: () {
onSelected(option);
},
child: ListTile(
title: Text(option.originalTitle!,
style: const TextStyle(
color: Colors.black87)),
),
);
},
),
),
),
);
}
},
inside optionviewbuilder i want am checking if fieldTextEditingController is empty then show data from sharedpreference if not show data from api but data only renders of sharedpreference not from api need some guidance here i am stuck
CodePudding user response:
what we can do is declare an string variable and inside textformfield it has property saying onChanged which takes value just assign that value to your declared string variable and use that declared variable to check if that variable is empty show shared preference data else show api data
eg:- String? data;
onChanged:(value){
data = value
}
then,
if(data == null || data.isEmpty){
show shared preference data
}else{
show apidata
}