【发布时间】: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 模式吗?
-
您好,是的,抱歉耽搁了。
标签: android firebase flutter widget