I'm using the package fuzzywuzzy from flutter and I don't know why but it says my result variable is null.
Can you guys help me and explain why?
I wanted the value of result I tried putting it in an await statement but it says result is null
Future<List<DocumentSnapshot>> performFuzzySearch(String searchText) async {
final querySnapshot = await FirebaseFirestore.instance.collection('Question Papers').get();
final searchResults = querySnapshot.docs.where((doc) {
print(doc["Keywords"]);
print(searchText);
final result = extractOne(query: searchText,
choices: doc["Keywords"],
cutoff: 10,
);
if (result.score >= 80) {
return true;
}
return true;
}).toList();
searchResults.sort(
(a, b) => b['Votes'].compareTo(
a['Votes'],
),
);
return searchResults;
}
My error is with result
If extractOne doesn't find a close enough match it will return a null. Check if the result is null before accessing its score property.
I've amended your code. Hopefully, this should work for you.
Future<List<DocumentSnapshot>> performFuzzySearch(String searchText) async {
final querySnapshot = await FirebaseFirestore.instance
.collection('Question Papers')
.get();
final searchResults = querySnapshot.docs.where((doc) {
print(doc["Keywords"]);
print(searchText);
final result = extractOne(
query: searchText,
choices: doc["Keywords"],
cutoff: 10,
);
if (result != null && result.score >= 80) {
return true;
}
return false;
}).toList();
searchResults.sort(
(a, b) => b['Votes'].compareTo(a['Votes']),
);
return searchResults;
}