I am creating a skype clone app in flutter and encountered an error when making the "search" functionality. I want to search my firebase db for other users by username or name returned in lowercase via a list that updates as you are typing. However, on first key stroke I encounter this error:
The method 'toLowerCase' was called on null. Receiver: null Tried calling: toLowerCase()
Here is the code for the search screen (the problem is under buildSuggestions):
`import 'package:chatbee/models/user.dart';
import 'package:chatbee/resources/firebase_repository.dart';
import 'package:chatbee/screens/chatscreens/chat_screen.dart';
import 'package:chatbee/utils/universal_variables.dart';
import 'package:chatbee/widgets/custom_tile.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:gradient_app_bar/gradient_app_bar.dart';
class SearchScreen extends StatefulWidget {
@OverRide
_SearchScreenState createState() => _SearchScreenState();
}
class _SearchScreenState extends State {
FirebaseRepository _repository = FirebaseRepository();
List userList;
String query = "";
TextEditingController searchController = TextEditingController();
@OverRide
void initState() {
// TODO: implement initState
super.initState();
_repository.getCurrentUser().then((FirebaseUser user) {
_repository.fetchAllUsers(user).then((List<User> list) {
setState(() {
userList = list;
});
});
});
}
searchAppBar(BuildContext context) {
return GradientAppBar(
backgroundColorStart: UniversalVariables.gradientColorStart,
backgroundColorEnd: UniversalVariables.gradientColorEnd,
leading: IconButton(
icon: Icon(Icons.arrow_back, color: Colors.white),
onPressed: () => Navigator.pop(context),
),
elevation: 0,
bottom: PreferredSize(
preferredSize: const Size.fromHeight(kToolbarHeight + 20),
child: Padding(
padding: EdgeInsets.only(left: 20),
child: TextField(
controller: searchController,
onChanged: (val) {
setState(() {
query = val;
});
},
cursorColor: UniversalVariables.blackColor,
autofocus: true,
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
fontSize: 35,
),
decoration: InputDecoration(
suffixIcon: IconButton(
icon: Icon(Icons.close, color: Colors.white),
onPressed: () {
WidgetsBinding.instance
.addPostFrameCallback((_) => searchController.clear());
},
),
border: InputBorder.none,
hintText: "Search",
hintStyle: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 35,
color: Color(0x88ffffff),
),
),
),
),
),
);
}
buildSuggestions(String query) {
final List suggestionList = query.isEmpty
? []
: userList.where((User user) {
String _getUsername = user.username.toLowerCase();
String _query = query.toLowerCase();
String _getName = user.name.toLowerCase();
bool matchesUsername = _getUsername.contains(_query);
bool matchesName = _getName.contains(_query);
return (matchesUsername || matchesName);
}).toList();
return ListView.builder(
itemCount: suggestionList.length,
itemBuilder: ((context, index) {
User searchedUser = User(
uid: suggestionList[index].uid,
profilePhoto: suggestionList[index].profilePhoto,
name: suggestionList[index].name,
username: suggestionList[index].username);
return CustomTile(
mini: false,
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ChatScreen(
receiver: searchedUser,
)));
},
leading: CircleAvatar(
backgroundImage: NetworkImage(searchedUser.profilePhoto),
backgroundColor: Colors.grey,
),
title: Text(
searchedUser.username,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
subtitle: Text(
searchedUser.name,
style: TextStyle(color: UniversalVariables.greyColor),
),
);
}),
);
}
@OverRide
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: UniversalVariables.blackColor,
appBar: searchAppBar(context),
body: Container(
padding: EdgeInsets.symmetric(horizontal: 20),
child: buildSuggestions(query),
),
);
}
}`