Exception has occurred.
NoSuchMethodError (NoSuchMethodError: The method ‘then’ was called on null.
Receiver: null
Tried calling: then(Closure: (dynamic) => Null))
Friends, I am getting the error I mentioned above. Does anyone know how to solve this?
I’m trying to user search via firebase.Does anyone know any other way to do this?
class _DiscoverScreen extends State<DiscoverScreen> {
DatabaseMethods databaseMethods = DatabaseMethods();
TextEditingController searchEditingController = TextEditingController();
QuerySnapshot? searchResultSnapshot;
@override
void dispose() {
searchEditingController.dispose();
super.dispose();
}
initiateSearch() async {
if (searchEditingController.text.isNotEmpty) {
await databaseMethods
.searchByName(searchEditingController.text)
.then((value) {
value.docs.forEach(
(result) {
searchResultSnapshot = value;
print("$searchResultSnapshot");
},
);
});
}
}
Widget userList() {
return searchResultSnapshot != null
? ListView.builder(
shrinkWrap: true,
itemCount: searchResultSnapshot!.docs.length,
itemBuilder: (context, index) {
return SearchTile(
name: searchResultSnapshot!.docs[index].get("name"),
username: searchResultSnapshot!.docs[index].get("username"),
image: searchResultSnapshot!.docs[index].get("photoUrl"),
);
})
: Container();
}
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Scaffold(
appBar: AppBar(
centerTitle: false,
title: Text("Keşfet"),
backgroundColor: Colors.grey.shade700,
),
body: Column(
children: [
Container(
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 16),
child: Row(
children: [
Expanded(
child: TextField(
decoration: InputDecoration(
border: InputBorder.none,
hintText: "Search username",
hintStyle: TextStyle(
color: Colors.black,
fontSize: 16,
),
),
style: TextStyle(
color: Colors.black,
fontSize: 20,
),
controller: searchEditingController,
),
),
IconButton(
onPressed: () {
initiateSearch();
},
icon: ImageIcon(
AssetImage(
"assets/icons/searchicon.png",
),
size: 20,
))
],
),
),
userList(),
],
),
);
}
}
searchByName Codes =
searchByName(String username) {
FirebaseFirestore.instance
.collection("users")
.where("username", isEqualTo: username)
.get();
}
2
Answers
It seems like
searchByName
is returning null. Either do a null check using the?
operator or provide a code sample with the code containingsearchByName
function in order to find the problem.we use method then with future mostly
make this databaseMethods Future type