方法“ toLowerCase”在null上被调用。

时间:2020-07-21 00:00:12

标签: firebase flutter dart google-cloud-firestore

我正在flutter应用程序上构建搜索功能,以从firebase中搜索用户。我知道,因为我尝试打印填充的列表。这里是打印语句[Instance of 'UserModel', Instance of 'UserModel', Instance of 'UserModel', Instance of 'UserModel']。我在Firebase上有4个用户,无法检索其所有数据,但是当我在搜索字段中键入字母时,会出现此错误。

    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尝试使用?运算符

 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);

但是我得到一个错误,说方法“包含”在null上被调用。 接收者:null 尝试调用: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”是我在键盘上输入的字母

1 个答案:

答案 0 :(得分:0)

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

这只会避免标题中的确切错误。要找出潜在的问题,请在此行上设置一个断点并以调试模式运行。程序在到达该行时将暂停。然后,您可以检查theVariableYouExpectToBeAString并确切地找出问题所在。