【发布时间】:2019-03-08 08:40:10
【问题描述】:
我有一个包含动态值的映射,其中一个可以是 int、string 或 null(即不存在的键)。
我想将此值存储在int 变量中。我想出的解析结果非常麻烦:
Map<String, dynamic> map = {'maxposts': null}; // or 23 or '42'
// Try to parse an int value or fall back to 0
int maxposts = (map['maxposts'] is int)
? map['maxposts']
: int.tryParse(map['maxposts'] ?? '0') ?? 0;
有没有更好的方法来进行这种“try-fallback”解析?
【问题讨论】:
-
我认为没有。
标签: dart flutter dynamic-typing