[英]LateInitializationError: Field has not been initialized in Flutter
I'm considerably new to Flutter and I'm to build a Messenger Chap App on Flutter, and I face the issue of "LateInitilization: Field 'searchSnapShot' has not been initialized. Following is the snippet of code that is causing the issue:我对 Flutter 相当陌生,我要在 Flutter 上构建一个 Messenger Chap 应用程序,我面临“LateInitilization:字段'searchSnapShot'尚未初始化的问题。以下是导致问题的代码片段:
Widget searchList() {
return searchSnapShot != null ? ListView.builder(
itemCount: searchSnapShot.docs.length,
shrinkWrap: true,
itemBuilder: (context, index) {
return SearchTile(
userName: searchSnapShot.docs[index].data()["name"],
userEmail: searchSnapShot.docs[index].data()["email"],
);
}
) : Container();
}
What this snippet is supposed to do is return a list of users that match the search query.这个片段应该做的是返回与搜索查询匹配的用户列表。 Following is the code for the entire search.dart :
以下是整个搜索的代码。dart :
import 'package:chat_app/services/database.dart';
import 'package:chat_app/widgets/widgets.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class SearchScreen extends StatefulWidget {
@override
_SearchScreenState createState() => _SearchScreenState();
}
class _SearchScreenState extends State<SearchScreen> {
DatabaseMethods databaseMethods = new DatabaseMethods();
TextEditingController searchTextEditingController = new TextEditingController();
late QuerySnapshot <Map<String, dynamic>> searchSnapShot;
initiateSearch() async {
await databaseMethods
.getUserByUsername(searchTextEditingController.text)
.then((val) {
setState(() {
searchSnapShot = val;
});
});
}
Widget searchList() {
return searchSnapShot != null ? ListView.builder(
itemCount: searchSnapShot.docs.length,
shrinkWrap: true,
itemBuilder: (context, index) {
return SearchTile(
userName: searchSnapShot.docs[index].data()["name"],
userEmail: searchSnapShot.docs[index].data()["email"],
);
}
) : Container();
}
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: appBarMain(context),
body: Container(
child: Column(
children: [
Container(
color: Color(0xffFFC200),
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 16),
child: Row(
children: [
Expanded(
child: TextField(
controller: searchTextEditingController,
decoration: InputDecoration(
hintText: "search username..",
hintStyle: TextStyle(
color: Colors.black,
),
border: InputBorder.none,
),
)
),
GestureDetector(
onTap: () {
initiateSearch();
},
child: Container(
height: 30,
child: Image.asset("assets/images/search_white.png")),
),
],
),
),
searchList()
],
),
),
);
}
}
class SearchTile extends StatelessWidget {
late final String userName;
late final String userEmail;
SearchTile({required this.userName, required this.userEmail});
@override
Widget build(BuildContext context) {
return Container(
child: Row(
children: [
Column(
children: [
Text(
userName,
style: simpleTextStyle()
),
Text(
userEmail,
style: simpleTextStyle(),
),
],
),
Spacer(),
Container(
decoration: BoxDecoration(
color: Colors.amber,
borderRadius: BorderRadius.circular(40)
),
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: Text("Message"),
)
],
),
);
}
}
Error Description in Android Studio: Android Studio 中的错误描述:
======== Exception caught by widgets library =======================================================
The following LateError was thrown building SearchScreen(dirty, state: _SearchScreenState#f41e2):
LateInitializationError: Field 'searchSnapShot' has not been initialized.
What am I doing wrong here?我在这里做错了什么? I'd really appreciate some help.
我真的很感激一些帮助。 Thank you.
谢谢你。
When you use late
it means that variable can be lazily initialised, So, before use it somewhere, you need to Initialise thus you cannot make null checks on that, If you want to make null checks on it then try ?
当您使用
late
时,这意味着变量可以被延迟初始化,因此,在某处使用它之前,您需要初始化,因此您无法对其进行空检查,如果您想对其进行空检查,请尝试?
which makes that variable a nullable.这使得该变量可以为空。
So remove late
and add ?
所以删除
late
并添加?
QuerySnapshot <Map<String, dynamic>>? searchSnapShot;
请试试这个
QuerySnapshot? <Map<String, dynamic>> searchSnapShot;
initialize searchSnapshot using:使用以下方法初始化 searchSnapshot:
QuerySnapshot<dynamic>? searchSnapshot;
also, add null checks in searchTitle:此外,在 searchTitle 中添加空检查:
SearchTitle(
userName: searchSnapshot!.docs[index].data()['name'],
userEmail: searchSnapshot!.docs[index].data()['email'],
);SearchTitle(
userName: searchSnapshot!.docs[index].data()['name'],
userEmail: searchSnapshot!.docs[index].data()['email'],
);
If you're using a setter under class make sure that the parameter in your setter is different from the variable you're initializing如果您在 class 下使用设置器,请确保设置器中的参数与您正在初始化的变量不同
Coupon? coupon;
void setCoupon(Coupon myCoupon){
coupon = myCoupon;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.