ERROR:flutter/runtime/dart_vm_initializer.cc(41) Unhandled Exception: type 'Null' is not a subtype of type 'DateTime'

huangapple go评论196阅读模式
英文:

ERROR:flutter/runtime/dart_vm_initializer.cc(41) Unhandled Exception: type 'Null' is not a subtype of type 'DateTime'

问题

我一直在尝试创建一个简单的API获取应用,突然间停止了获取。调试控制台显示以下错误。

代码

调试错误

第一次它运行得很好,现在我无法确定问题所在。请帮我解决这个问题。

英文:

I have been trying to create a simple API fetching app and suddenly stoped getting fetched. Debug console shows the below error.
ERROR:flutter/runtime/dart_vm_initializer.cc(41) Unhandled Exception: type 'Null' is not a subtype of type 'DateTime'

Codes

ERROR:flutter/runtime/dart_vm_initializer.cc(41) Unhandled Exception: type 'Null' is not a subtype of type 'DateTime'

Debug error

ERROR:flutter/runtime/dart_vm_initializer.cc(41) Unhandled Exception: type 'Null' is not a subtype of type 'DateTime'

First time it worked fine and now I couldn't identify the issue. Pls. help me with this

答案1

得分: 1

更好的做法是在 DateTime? 上使用可为空的数据类型。这样你可以接受空值,并在使用时进行空值检查。

final DateTime? bdate;

另外,你可以在空值情况下提供默认的日期时间,但这不是推荐的做法。

bdate: json['bdate'] ?? DateTime.now(), ///在空值情况下使用当前日期
英文:

Better will be having nullable dataType on DateTime?. this way you can accept null value and do a null check UI while using it.

final DateTime? bdate;

Also you can provide default dateTime on null-case which isn't preferable

bdate: json['bdate']?? DateTime.now(), ///having current date on null case

huangapple
  • 本文由 发表于 2023年2月27日 02:37:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定