gpt4 book ai didi

android - 后期初始化错误 : Field has not been initialized in Flutter

转载 作者:行者123 更新时间:2023-12-04 23:38:59 24 4
gpt4 key购买 nike

我对 Flutter 相当陌生,我要在 Flutter 上构建一个 Messenger Chap 应用程序,我面临“LateInitilization:字段'searchSnapShot'尚未初始化的问题。以下是导致问题的代码 fragment :

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();
}
这个 fragment 应该做的是返回与搜索查询匹配的用户列表。以下是整个 的代码搜索.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"),
)
],
),
);
}
}
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.
我在这里做错了什么?我真的很感激一些帮助。谢谢你。

最佳答案

当您使用 late这意味着变量可以被延迟初始化,因此,在某处使用它之前,您需要初始化,因此您不能对其进行空检查,如果您想对其进行空检查,请尝试 ?这使得该变量可以为空。
所以删除 late并添加 ?

QuerySnapshot <Map<String, dynamic>>? searchSnapShot;

关于android - 后期初始化错误 : Field has not been initialized in Flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69309379/

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