最近几天,我正在尝试解决此问题。无法解决此问题。这是一个简单的聊天应用程序,我正在尝试进行实验。因此,当我发送任何消息时,就会显示此错误并立即消失。其他一切工作正常。甚至检索消息并发送消息也可以正常工作。只是显示了检索消息和发送消息之间的错误。谁能解释我为什么会收到此错误? 我的代码在这里:
import 'package:flutter/material.dart';
import 'package:flash_chat/constants.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/rendering.dart';
import 'package:intl/intl.dart';
final _fireStore = Firestore.instance;
final _auth = FirebaseAuth.instance;
FirebaseUser loggedInUser;
class ChatScreen extends StatefulWidget {
static String id = 'chat_screen';
@override
_ChatScreenState createState() => _ChatScreenState();
}
class _ChatScreenState extends State<ChatScreen> {
final messageTextController = TextEditingController();
String messageText;
@override
void initState() {
super.initState();
getCurrentUser();
}
void getCurrentUser() async {
try {
final user = await _auth.currentUser();
if (user != null) {
loggedInUser = user;
print(loggedInUser.email);
}
} catch (e) {
print(e);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
leading: null,
actions: <Widget>[
IconButton(
icon: Icon(Icons.close),
onPressed: () {
_auth.signOut();
Navigator.pop(context);
}),
],
title: Text('⚡️Chat'),
backgroundColor: Colors.lightBlueAccent,
),
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
child: MessageStream(),
),
Container(
decoration: kMessageContainerDecoration,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(
child: TextField(
controller: messageTextController,
style: TextStyle(
color: Colors.black,
),
onChanged: (value) {
messageText = value;
},
decoration: kMessageTextFieldDecoration,
),
),
FlatButton(
onPressed: () async {
messageTextController.clear();
final fieldValue = FieldValue.serverTimestamp();
await _fireStore.collection('messages').add({
"sender": loggedInUser.email,
"text": messageText,
"messageTime": fieldValue,
});
},
child: Text(
'Send',
style: kSendButtonTextStyle,
),
),
],
),
),
],
),
),
);
}
}
class MessageStream extends StatelessWidget {
@override
Widget build(BuildContext context) {
Timestamp serverTime;
//var timeDate;
return StreamBuilder<QuerySnapshot>(
stream: _fireStore
.collection('messages')
.orderBy('messageTime', descending: false)
.snapshots(),
builder: (context, snapshot) {
if (snapshot.data == null) {
return Center(
child: CircularProgressIndicator(
backgroundColor: Colors.lightBlueAccent,
),
);
} else {
final messages = snapshot.data.documents.reversed;
List<MessageBubble> messageBubbles = [];
for (var message in messages) {
final messageText = message.data['text'];
final sender = message.data['sender'];
serverTime = message.data['messageTime'];
print(serverTime);
// final seconds = serverTime.seconds;
// final nanoSeconds = serverTime.nanoseconds;
DateTime timeDate = serverTime?.toDate() ?? null;
final time = DateFormat.jm().format(timeDate);
print(time);
if (time != null) {
final currentUser = loggedInUser.email;
final messageBubble = MessageBubble(
message: messageText,
sender: sender,
isMe: currentUser == sender,
messageTime: time.toString(),
);
messageBubbles.add(messageBubble);
}
}
return ListView(
scrollDirection: Axis.vertical,
reverse: true,
children: messageBubbles,
);
}
},
);
}
}
class MessageBubble extends StatelessWidget {
MessageBubble({this.sender, this.message, this.isMe, this.messageTime});
final String message;
final String sender;
final bool isMe;
final String messageTime;
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment:
isMe ? CrossAxisAlignment.end : CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: isMe
? EdgeInsets.only(
right: 20,
)
: EdgeInsets.only(
left: 20,
),
child: Text(
sender,
style: TextStyle(color: Colors.black45),
),
),
Padding(
padding: EdgeInsets.symmetric(
horizontal: 10,
vertical: 5,
),
child: Material(
borderRadius: isMe
? BorderRadius.only(
topLeft: Radius.circular(20),
bottomLeft: Radius.circular(20),
bottomRight: Radius.circular(20),
)
: BorderRadius.only(
topRight: Radius.circular(20),
bottomLeft: Radius.circular(20),
bottomRight: Radius.circular(20),
),
elevation: 5,
color: isMe ? Colors.lightBlueAccent : Colors.white,
child: Padding(
padding: EdgeInsets.symmetric(
horizontal: 15,
vertical: 15,
),
child: Column(
children: <Widget>[
Text(
message,
style: TextStyle(
fontSize: 15,
),
),
Text(
messageTime,
style: TextStyle(
fontSize: 10,
color: Colors.black54,
),
),
],
),
),
),
),
SizedBox(
height: 10,
),
],
);
}
}
这是我得到的错误,该错误在1秒后消失。
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building StreamBuilder<QuerySnapshot>(dirty, state: _StreamBuilderBaseState<QuerySnapshot, AsyncSnapshot<QuerySnapshot>>#c7c89):
The getter 'hour' was called on null.
Receiver: null
Tried calling: hour
The relevant error-causing widget was:
StreamBuilder<QuerySnapshot> file:///J:/My%20Documents/flutter/flash-chat-flutter/lib/screens/chat_screen.dart:114:12
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1 _DateFormatPatternField.format1To12Hours (package:intl/src/intl/date_format_field.dart:507:22)
#2 _DateFormatPatternField.formatField (package:intl/src/intl/date_format_field.dart:357:16)
#3 _DateFormatPatternField.format (package:intl/src/intl/date_format_field.dart:244:12)
#4 DateFormat.format (package:intl/src/intl/date_format.dart:276:26)
...
这是我的数据的快照。尽管数据和一切都很好,但仍显示错误。
I/flutter (20608): {messageTime: Timestamp(seconds=1596923950, nanoseconds=769000000), sender: rony.yf25@gmail.com, text: rrr}
I/flutter (20608): {messageTime: Timestamp(seconds=1596919907, nanoseconds=674000000), sender: rony.yf25@gmail.com, text: tttt}
I/flutter (20608): {messageTime: Timestamp(seconds=1596919884, nanoseconds=184000000), sender: rony.yf25@gmail.com, text: hello}
I/flutter (20608): {messageTime: Timestamp(seconds=1596918220, nanoseconds=934000000), sender: rony.yf25@gmail.com, text: alsdifjalse}
I/flutter (20608): {messageTime: Timestamp(seconds=1596918137, nanoseconds=200000000), sender: rony.yf25@gmail.com, text: alsidfjasdf}
I/flutter (20608): {messageTime: Timestamp(seconds=1596918011, nanoseconds=857000000), sender: rony.yf25@gmail.com, text: slifdjai}
I/flutter (20608): {messageTime: Timestamp(seconds=1596917828, nanoseconds=410000000), sender: rony.yf25@gmail.com, text: sdlifa}
I/flutter (20608): {messageTime: Timestamp(seconds=1596917781, nanoseconds=912000000), sender: rony.yf25@gmail.com, text: dhalsid}
I/flutter (20608): {messageTime: Timestamp(seconds=1596917481, nanoseconds=457000000), sender: rony.yf25@gmail.com, text: alsidfjisd}
I/flutter (20608): {messageTime: Timestamp(seconds=1596917410, nanoseconds=747000000), sender: rony.yf25@gmail.com, text: alsiefj}
I/flutter (20608): {messageTime: Timestamp(seconds=1596917377, nanoseconds=497000000), sender: rony.yf25@gmail.com, text: hey}
I/flutter (20608): {messageTime: Timestamp(seconds=1596917078, nanoseconds=43000000), sender: rony.yf25@gmail.com, text: alisdjf}
I/flutter (20608): {messageTime: Timestamp(seconds=1596917009, nanoseconds=664000000), sender: rony.yf25@gmail.com, text: dlfjie}
I/flutter (20608): {messageTime: Timestamp(seconds=1596915940, nanoseconds=43000000), sender: rony.yf25@gmail.com, text: what the hell}
I/flutter (20608): {messageTime: Timestamp(seconds=1596915855, nanoseconds=204000000), sender: rony.yf25@gmail.com, text: hello}
I/flutter (20608): {messageTime: Timestamp(seconds=1596915359, nanoseconds=85000000), sender: rony.yf25@gmail.com, text: now?}
I/flutter (20608): {messageTime: Timestamp(seconds=1596915336, nanoseconds=709000000), sender: rony.yf25@gmail.com, text: what the hell?}
I/flutter (20608): {messageTime: Timestamp(seconds=1596915316, nanoseconds=263000000), sender: rony.yf25@gmail.com, text: sdlifj}
I/flutter (20608): {messageTime: Timestamp(seconds=1596915263, nanoseconds=685000000), sender: rony.yf25@gmail.com, text: sljeif}
I/flutter (20608): {messageTime: Timestamp(seconds=1596914949, nanoseconds=530000000), sender: rony.yf25@gmail.com, text: alsdifj}
I/flutter (20608): {messageTime: Timestamp(seconds=1596914912, nanoseconds=147000000), sender: rony.yf25@gmail.com, text: alkjsfdil}
I/flutter (20608): {messageTime: Timestamp(seconds=1596914381, nanoseconds=39000000), sender: rony.yf25@gmail.com, text: kdhsoi}
I/flutter (20608): {messageTime: Timestamp(seconds=1596914309, nanoseconds=901000000), sender: rony.yf25@gmail.com, text: sldkfjakljhf}
I/flutter (20608): {messageTime: Timestamp(seconds=1596914266, nanoseconds=63000000), sender: rony.yf25@gmail.com, text: soooo}
I/flutter (20608): {messageTime: Timestamp(seconds=1596914160, nanoseconds=123000000), sender: rony.yf25@gmail.com, text: hello again}
I/flutter (20608): {messageTime: Timestamp(seconds=1596914115, nanoseconds=909000000), sender: rony.yf25@gmail.com, text: hello}
I/flutter (20608): {messageTime: Timestamp(seconds=1596914034, nanoseconds=646000000), sender: rony.yf25@gmail.com, text: hello}
I/flutter (20608): {messageTime: Timestamp(seconds=1596913757, nanoseconds=969000000), sender: rony.yf25@gmail.com, text: hello again}
I/flutter (20608): {messageTime: Timestamp(seconds=1596913551, nanoseconds=830000000), sender: rony.yf25@gmail.com, text: hello}