如何解决这个 Jiffy 构造函数错误?

问题描述 投票:0回答:1

我不确定在此用例中添加的最佳构造函数。 仍在尝试弄清楚 flutter,所以我在尝试构建应用程序时陷入了困境。

我尝试了不同的构造函数,例如“jiffy.datetime”“jiffy.parsejiffy”等,但它们似乎没有解决问题,而是不断返回相同的错误。enter image description here

请帮忙

flutter dart constructor flutter-dependencies jiffy
1个回答
0
投票

根据您的代码,您似乎错误地使用了 Jiffy。从版本

6.0.0
Jiffy 不允许仅通过调用构造函数来创建其实例

你必须使用工厂方法来创建一个

所以下面我重写了您的代码来解决您遇到的问题

// This looks great 
Jiffy createAt = Jiffy.parseFromDateTime(widget.datetime);

// On line 277
// Rather than
final now = Datetime.now() // remove this
// Use Jiffy
final now = Jiffy.now() // Now you have a Jiffy instance

// If you want to get a Datetime from a Jiffy, just write
now.datetime; // This will return a Datetime object/instance


// On line 279
// Rather than creating another instance of Jiffy
Jiffy.parseFromJiffy(createAt).isSame(now, Units.day) // remove this
// Use the createdAt since it a Jiffy instance itself
createdAt.isSame(now, Units.day) // replace it with this

// And one additional thing, the `isSame` methods takes in a Jiffy 
// object/instance, not a Datetime object


// On line 281 - 282
// Rather than
Jiffy(createdAt).isSame(now.subtract(const Duration(days: 1), Units.day))
// Replace it with
createdAt.isSame(now.subtract(days: 1), Units.day))


// On line 284 - 286
// Rather than
Jiffy(createAt).isAfter(now.subtract(const Duration(days: 7), Units.day))
// Replace it with
createdAt.isAfter(now.subtract(days: 7), Units.day))
// or you can use `week: 1` also that Jiffy supports
createdAt.isAfter(now.subtract(weeks: 1), Units.day))


// On line 290 - 291
// Rather than 
Jiffy(createdAt).isAfter(Jiffy(now).subtract(years: 1), Units.day))
// Replace it with
createdAt.isAfter(now.subtract(years: 1), Units.day))

希望这对如何使用Jiffy有帮助,如果您还有其他问题,请随时询问

© www.soinside.com 2019 - 2024. All rights reserved.