I'm building a search feature on my flutter app to search for users from firebase.I know becasue i tried prininting the list that i populated.Here's the print statement [Instance of 'UserModel', Instance of 'UserModel', Instance of 'UserModel', Instance of 'UserModel']. I have 4 users on firebase and im able to retrieve all their data but when i type a letter in the search field I get this error.
The following NoSuchMethodError was thrown building SearchScreen(dirty, state: _SearchScreenState#6b085): The method 'toLowerCase' was called on null. Receiver: null Tried calling: toLowerCase()
I pasted the complete log and code on pastebin as stackoverflow would let me post with the complete code.
https://pastebin.com/mFqvzh1v.I tried using ? operator
String _getUsername = user.displayName?.toLowerCase();
String _query = query?.toLowerCase();
String _getName = user.username?.toLowerCase();
bool matchesUsername = _getUsername.contains(_query)?? false;
bool matchesName = _getName.contains(_query);
return (matchesUsername || matchesName);
but im getting an error saying The method 'contains' was called on null. Receiver: null Tried calling: contains("m")
The relevant error-causing widget was:
SearchScreen file:///D:/fashow/lib/Timeline.dart:632:86
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1 _SearchScreenState.buildSuggestions.<anonymous closure> (package:fashow/Search_screen.dart:123:43)
#2 WhereIterator.moveNext (dart:_internal/iterable.dart:442:13)
#3 new List.from (dart:core-patch/array_patch.dart:50:19)
#4 new List.of (dart:core-patch/array_patch.dart:68:17)
"m" was the letter i typed on the keyboard
theVariableYouExpectToBeAString.toLowerCase()the theVariableYouExpectToBeAString is null not a string - Yadu