【发布时间】:2020-11-11 01:43:18
【问题描述】:
我有一个列表,其中有一个名为 updatedAt 的参数,它是一个具有 DateTime.now().toString().substring(0, 19) 的字符串,我需要组织按日期(最新(位置 0)到最旧(最后位置))但是当我尝试使用list.sort((a, b) => a.updatedAt.compareTo(b.updatedAt))我有下面的错误,我必须将String转换为DateTime还是什么?
class Product {
String id;
String image;
String name;
String desc;
int price;
int quantity;
String category;
String createdAt;
String updatedAt;
bool active;
Product({this.id, this.image, this.name, this.desc, this.price, this.quantity,
this.category, this.createdAt, this.updatedAt, this.active}){
quantity = 1;
active = true;
}
Future<void> getProducts() async {
List<Product> products = [];
products.addAll(await getProductsFromFirestore()); // Here I add products from Firebase...
print(products[0].updatedAt); // 2020-07-21 12:31:08
print(products[1].updatedAt); // 2020-07-21 12:31:13
products.sort((a, b) => a.updatedAt.compareTo(b.updatedAt));
}
错误(Flutter Web):
Error: Expected a value of type 'Comparable<dynamic>', but got one of type 'Product'
at Object.throw_ [as throw] (http://localhost:53122/dart_sdk.js:4339:11)
at Object.castError (http://localhost:53122/dart_sdk.js:4310:15)
at Object.cast [as as] (http://localhost:53122/dart_sdk.js:4626:17)
at Function.as_C [as as] (http://localhost:53122/dart_sdk.js:4271:19)
at http://localhost:53122/dart_sdk.js:15079:98
at Function._insertionSort (http://localhost:53122/dart_sdk.js:23099:55)
at Function._doSort (http://localhost:53122/dart_sdk.js:23086:24)
at Function.sort (http://localhost:53122/dart_sdk.js:23068:22)
at Array.[dartx.sort] (http://localhost:53122/dart_sdk.js:15079:26)
at RxList.new.sort (http://localhost:53122/packages/get/src/rx/rx_impl.dart.lib.js:610:27)
at home_controller.HomeController.new.getProductsFromController (http://localhost:53122/packages/site_catalogo_modelo/controllers/home_controller.dart.lib.js:94:21)
at product_controller.ProductController.new.getProducts (http://localhost:53122/packages/site_catalogo_modelo/controllers/home_controller.dart.lib.js:212:74)
at getProducts.next (<anonymous>)
at http://localhost:53122/dart_sdk.js:37340:33
at _RootZone.runUnary (http://localhost:53122/dart_sdk.js:37194:58)
at _FutureListener.thenAwait.handleValue (http://localhost:53122/dart_sdk.js:32178:29)
at handleValueCallback (http://localhost:53122/dart_sdk.js:32725:49)
at Function._propagateToListeners (http://localhost:53122/dart_sdk.js:32763:17)
at _Future.new.[_completeWithValue] (http://localhost:53122/dart_sdk.js:32606:23)
at async._AsyncCallbackEntry.new.callback (http://localhost:53122/dart_sdk.js:32628:35)
at Object._microtaskLoop (http://localhost:53122/dart_sdk.js:37455:13)
at _startMicrotaskLoop (http://localhost:53122/dart_sdk.js:37461:13)
at http://localhost:53122/dart_sdk.js:32980:9
【问题讨论】:
-
你能给我完整的代码吗,比如你存储的整个价值products,还有,你为什么不试试
products.sort()看看是不是这样你想要什么?我觉得这很有趣,我们可以一起努力,只需给我更多数据:) -
我看不出所提供的代码有什么问题; it works fine in DartPad。您确定这是相关代码并且您完全复制并粘贴了吗?
-
我刚刚更新了添加整个类并模拟过程的代码,我只是将来自 firestore 的数据添加到产品列表中,问题是当我尝试比较这两个字符串时 DateTime子字符串。
-
@Alok
products.sort()不起作用,因为Product没有实现Comparable接口。 -
@djalmafreestyler
String已经实现了Comparable接口。你的问题在别处。请创建一个最小的、可重现的示例。这并不意味着您需要展示您的 Firestore 代码;这意味着您需要制作一个 simplified 示例(例如,与我链接到的 DartPad 示例进行比较)。
标签: list sorting flutter dart flutter-web