gpt4 book ai didi

android - flutter 错误 "the method ' toLowerCase( )' was called on null"

转载 作者:行者123 更新时间:2023-12-05 00:05:26 30 4
gpt4 key购买 nike

我正在 flutter 中创建一个 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 不为空时运行 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),
),
);
}

关于android - flutter 错误 "the method ' toLowerCase( )' was called on null",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59568921/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com