firebase - 在空上调用了 'toLowerCase'方法。

标签 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
尝试调用: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”是我在键盘上输入的字母

最佳答案

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

关于firebase - 在空上调用了 'toLowerCase'方法。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63005451/

相关文章:

animation - Matrix4Tween 无法正常工作?

flutter - 如何在 flutter 中打开相机闪光灯?

dart - 似乎没有 polymer 布局事件

c# - 从 Google 身份验证服务为 .NET 中的 REST 服务检索新的 Firebase 访问 token

javascript - 当应用程序处于后台时尝试使用 AsyncStorage 写入获取的数据库数据时出现 iOS 错误

android - 使用 Firebase UI 依赖项的重复条目 Gradle 错误

java - 为什么我不能在查询 Firestore 中使用 whereEqualTo 条件

android - flutter 找到了多个文件,操作系统独立路径为 'AndroidManifest.xml'

flutter - 将 int 数据转换为字符串返回 null - Flutter Firestore

firebase - 如何使用flutter查询firestore中的子集合?