作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 开发一个聊天应用程序火力基地 .
有一段时间我收到此错误 Unhandled Exception: NoSuchMethodError: The method 'toDate' was called on null.
转换时出错 Timestamp
至DateTime
使用 toDate()
.
在一段时间内出现错误,然后错误消失。
请考虑这个 GIF .
这是火灾存储数据类型为 TimeStamp
.
源代码
class ChatMessageModel {
bool isSentByMe;
String msg = '';
///SERVER TIME
Timestamp time;
DateTime localTime;
String timeStamp;
String fromUid;
String toUid;
ChatMessageModel._();
ChatMessageModel.fromSnapshot(DocumentSnapshot snapshot) {
this.isSentByMe =
snapshot.data['from'] == LoginBloc.instance.firebaseUser.uid;
this.msg = snapshot.data['msg'];
this.timeStamp = snapshot.data['time'].toString();
this.time = snapshot.data['time'];
this.fromUid = snapshot.data['from'];
this.toUid = snapshot.data['to'];
this.localTime = this.time.toDate(); //ERROR
}
}
最佳答案
它显示错误,因为在 this.time 的调用时间,
this.time 为空。所以 toDate() 函数不能应用于 null
尝试更改:this.localTime = this.time.toDate();
至:
this.localTime = this.time == null ? DateTime.now()
: this.time.toDate();
或者你可以这样使用:
String stringtime = widget.time == null
? DateTime.now().toString()
: widget.time.toDate().toString();
// stringtime = widget.time.toDate().toString();
DateTime date = DateTime.parse(stringtime);
String clocktime = DateFormat('hh:mm a').format(date);
希望它对我有用:)
关于dart - 未处理的异常 : NoSuchMethodError: The method 'toDate' was called on null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54985317/
我是一名优秀的程序员,十分优秀!