2
votes

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

1
You should look at if all of the user models your are getting back have displayName set, as that would be my first guess, or query is null. - Garrett Barlocker
Please create a minimal, complete and verifiable example and post it directly into your question. - Christopher Moore
it is because, in theVariableYouExpectToBeAString.toLowerCase() the theVariableYouExpectToBeAString is null not a string - Yadu
@GarrettBarlocker yes they do - marc walton
@neuromancer i have no idea what to do about it - marc walton

1 Answers

0
votes
theVariableYouExpectToBeAString?.toLowerCase() ?? 'OtherStringJustInCase';

This will only avoid the exact error in your title. To find out the underlying problem set a breakpoint on this line and run in debug mode. The program will pause when it hits that line. Then you can inspect theVariableYouExpectToBeAString and see exactly what/where the issue is.