父 Widget 的使用不正确

19 浏览
0 Comments

父 Widget 的使用不正确

我正在尝试制作一个闪聊应用程序,它可以从fireBase检索聊天记录并在屏幕上显示。我将其包装在一个Expanded小部件下面,并对其进行了一些填充。\n我遇到了以下错误:\n查找父数据时抛出了以下断言:\nParentDataWidget的使用不正确。\n以下是代码:\nimport \'package:flutter/cupertino.dart\';\nimport \'package:flutter/material.dart\';\nimport \'package:flashchat1/constants.dart\';\nimport \'package:firebase_auth/firebase_auth.dart\';\nimport \'package:cloud_firestore/cloud_firestore.dart\';\nclass ChatScreen extends StatefulWidget {\n static String id=\'Chat_Screen\';\n \n @override\n _ChatScreenState createState() => _ChatScreenState();\n}\nclass _ChatScreenState extends State {\n final _fireStore = FirebaseFirestore.instance;\n final _auth = FirebaseAuth.instance;\n late User loggedInUser;\n late String messageText;\n @override\n void initState() {\n super.initState();\n getCurrentUser();\n }\n void getCurrentUser() async {\n try {\n final user = await _auth.currentUser;\n if (user != null) {\n loggedInUser = user;\n print(loggedInUser.email);\n }\n } catch(e) {\n print(e);\n }\n }\n Future messageStream() async {\n await for (var snapshot in _fireStore.collection(\'messages\').snapshots()) {\n for (var message in snapshot.docs) {\n print(message.data());\n }\n }\n }\n void getMessages() async {\n final messages = await _fireStore.collection(\'messages\').get();\n messages.docs;\n for (var message in messages.docs) {\n print(message.data());\n }\n }\n Widget build(BuildContext context) {\n return Scaffold(\n appBar: AppBar(\n leading: null,\n actions: [\n IconButton(\n icon: Icon(Icons.close),\n onPressed: () {\n messageStream();\n //_auth.signOut();\n //Navigator.pop(context);\n //Implement logout functionality\n },\n ),\n ],\n title: Text(\'Chat\'),\n backgroundColor: Colors.lightBlueAccent,\n ),\n body: SafeArea(\n child: Column(\n mainAxisAlignment: MainAxisAlignment.spaceBetween,\n crossAxisAlignment: CrossAxisAlignment.stretch,\n children: [\n Expanded(\n child: StreamBuilder(\n stream: _fireStore.collection(\'messages\').snapshots(),\n builder: (context, AsyncSnapshot snapshot) {\n if (!snapshot.hasData) {\n return Center(\n child: CircularProgressIndicator(\n backgroundColor: Colors.lightBlueAccent,\n ),\n );\n }\n final messages = snapshot.data.docs;\n List messageWidgets = [];\n for (var message in messages) {\n final messageText = message.data()[\'text\'];\n final messageSender = message.data()[\'Sender\'];\n final messageWidget = Text(\'$messageText from $messageSender\',\n style: TextStyle(\n fontSize: 50,\n ),\n );\n messageWidgets.add(messageWidget);\n }\n return Expanded(\n flex: 2,\n child: ListView(\n children: messageWidgets,\n padding: EdgeInsets.symmetric(horizontal: 5, vertical: 5),\n ),\n );\n },\n ),\n ),\n Container(\n decoration: kMessageContainerDecoration,\n child: Row(\n crossAxisAlignment: CrossAxisAlignment.center,\n children: [\n Expanded(\n child: TextField(\n onChanged: (value) {\n messageText = value;\n },\n decoration: kMessageTextFieldDecoration,\n ),\n ),\n FlatButton(\n onPressed: () {\n _fireStore.collection(\'messages\').add({\n \'text\': messageText,\n \'Sender\': loggedInUser.email,\n },);\n },\n child: Text(\n \'Send\',\n style: kSendButtonTextStyle,\n ),\n ),\n ],\n ),\n ),\n ],\n ),\n ),\n );\n }\n}\n\n请注意,这只是一个初步的翻译版本,可能需要根据上下文和具体环境进行进一步的修改和调整。

0
0 Comments

这段代码中出现了"父部件的错误使用"问题。出现这个问题的原因是代码中将Expanded部件放在了不允许使用的位置。Expanded部件只能在Row或Column部件中使用。

解决方法是将上述代码块中的Expanded部件移除即可解决问题。

0