错误:参数类型“DateTime?“无法分配给参数类型”DateTime“。
@override Widget build(BuildContext context) { final formatter = LastUpdatedDateFormatter( lastUpdated: _endpointsData != null ? _endpointsData.values[Endpoint.cases]!.date : null, );
xzlaal3s1#
类型声明旁边的?表示变量可以为空,也就是说它可以接受null值举例来说:String? a = null是有效的,而String bar = null不是有效的语句。在表达式lastUpdated: _endpointsData != null ? _endpointsData.values[Endpoint.cases]!.date : null中,lastUpdated参数有可能接收null值,但它不期望或接受该值。它需要接收一个DateTime对象。考虑使用默认值,例如-DateTime.now()而不是null:
?
null
String? a = null
String bar = null
lastUpdated: _endpointsData != null ? _endpointsData.values[Endpoint.cases]!.date : null
lastUpdated
DateTime
DateTime.now()
@override Widget build(BuildContext context) { final formatter = LastUpdatedDateFormatter( lastUpdated: _endpointsData != null ? _endpointsData.values[Endpoint.cases]!.date : DateTime.now(), );
有关更多信息,请参阅https://dart.dev/null-safety上的Dart文档
hjqgdpho2#
已解决:我删除了语句中的null,然后将其转换为DateTime。最新更新:_endpointsData.values[Endpoint.cases]?.date作为DateTime,
2条答案
按热度按时间xzlaal3s1#
类型声明旁边的
?
表示变量可以为空,也就是说它可以接受null
值举例来说:
String? a = null
是有效的,而String bar = null
不是有效的语句。在表达式
lastUpdated: _endpointsData != null ? _endpointsData.values[Endpoint.cases]!.date : null
中,lastUpdated
参数有可能接收null
值,但它不期望或接受该值。它需要接收一个DateTime
对象。考虑使用默认值,例如-DateTime.now()
而不是null:有关更多信息,请参阅https://dart.dev/null-safety上的Dart文档
hjqgdpho2#
已解决:我删除了语句中的null,然后将其转换为DateTime。
最新更新:_endpointsData.values[Endpoint.cases]?.date作为DateTime,