颤振错误“方法'toLowerCase()'在null上被调用”
Posted
技术标签:
【中文标题】颤振错误“方法\'toLowerCase()\'在null上被调用”【英文标题】:Flutter error "the method 'toLowerCase()' was called on null"颤振错误“方法'toLowerCase()'在null上被调用” 【发布时间】:2020-04-21 10:45:05 【问题描述】:我正在颤振中创建一个 Skype 克隆应用程序,并在创建“搜索”功能时遇到错误。我想通过在您键入时更新的列表以小写形式返回的用户名或名称在我的 firebase 数据库中搜索其他用户。但是,在第一次击键时,我遇到了这个错误:
The method 'toLowerCase' was called on null.
Receiver: null
Tried calling: toLowerCase()
这是搜索屏幕的代码(问题在buildSuggestions下):
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:gradient_app_bar/gradient_app_bar.dart';
import 'package:skypeclone1/models/user.dart';
import 'package:skypeclone1/resources/firebase_repository.dart';
import 'package:skypeclone1/utils/universal_variables.dart';
import 'package:skypeclone1/widgets/custom_tile.dart';
class SearchScreen extends StatefulWidget
@override
_SearchScreenState createState() => _SearchScreenState();
class _SearchScreenState extends State<SearchScreen>
FirebaseRepository _repository = FirebaseRepository();
List<User> 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<User> 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: () ,
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),
),
);
如果我从 buildSuggestions 中省略了 toLowerCase() 小部件,我会收到类似的错误:
The method 'contains' was called on null.
Receiver: null
Tried calling: contains("h")
*h 是我的按键
【问题讨论】:
这是userList = list;
空吗?检查使用 print(userList);
当我打印时,输出是I/flutter ( 7963): [Instance of 'User', Instance of 'User', Instance of 'User']
。抱歉,我对此很陌生。
你在使用 bloc 模式吗?
您好,是的,抱歉耽搁了。
【参考方案1】:
你的构建函数应该是这样的,它只在 userList 不为空时运行 buildSuggestions。
@override
Widget build(BuildContext context)
return Scaffold(
backgroundColor: UniversalVariables.blackColor,
appBar: searchAppBar(context),
body: Container(
padding: EdgeInsets.symmetric(horizontal: 20),
child: userList.isEmpty?Container():buildSuggestions(query),
),
);
【讨论】:
嗨,同样的错误被抛出。分享我的 main.dart 文件有帮助吗?【参考方案2】:您需要验证您的输入数据。因为您的某些输入数据可以为空,所以会出现错误 你应该检查这个代码:
final List<User> 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();
或者你可以如下修复:
userList.where((User user)
if (query != null || user.username != null || user.name != null)
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);
else return false;
).toList()
【讨论】:
我应用了这个修复并且抛出了同样的错误。也许我应该在其他地方写一个 else 语句?我也应该在此处包含我的 main.dart 文件吗?【参考方案3】:嘿,在上面的代码中,错误
方法 'toLowerCase'
在 null 上被调用。
接收者:null 尝试调用:toLowerCase()
是由于数据库中的用户数据不规则造成的
要解决这个问题,请尝试删除数据库中的用户集合或删除不规则的集合并重新登录
【讨论】:
以上是关于颤振错误“方法'toLowerCase()'在null上被调用”的主要内容,如果未能解决你的问题,请参考以下文章
升级颤振后颤振标签栏错误“getter'key'被称为null”
命令“颤振:新项目”导致错误(找不到命令“颤振.createProject”),我无法在 VSCODE 上调试颤振项目