Listview构建器未获取完整的列表图数据Flutter

时间:2019-07-14 14:58:45

标签: flutter dart

我有一组List>数据,其中包含值名称,数量和产品日期。我正在将数据传递到listview构建器中,以呈现为每种产品的卡片列表。

通过的产品列表:

  

[{{name:Product 1,数量:24.5,date:2019-07-14 00:00:00.000},{name:   产品2,金额:29.5,日期:2019-07-14 00:00:00.000},{名称:产品3,   金额:29.5,日期:2019-07-15 00:00:00.000}]

列表视图工作正常,但是我试图添加一个函数,其中列表视图按创建日期过滤产品。为此,我要通过日期时间选择器传递日期(使用setState,默认值为 DateTime.now()),然后将产品日期与从日期时间选择器中选择的日期进行比较。

请在下面找到代码:

import 'package:flutter/material.dart';
import 'package:learning_app/product.dart';
import 'package:intl/intl.dart';

class BuildProduct extends StatelessWidget {
  final List<Map<String, dynamic>> products;
  final Function updateProduct;
  final DateTime selectedDate;

  Tasks(this.products, this.updateProduct, this.selectedDate);

  Widget _buildCard() {
    DateTime _currentDate = DateTime.now();

    Widget ProductCard = Center(
      child: Text('No Products added'),
    );
    if (products.length > 0) {
      ProductCard = ListView.builder(
        scrollDirection: Axis.vertical,
        shrinkWrap: true,
        itemCount: products.length,
        itemBuilder: (BuildContext context, int index) {
          print("Product List: " + products.toString());
          print("Product Name: " + products[index]['name'].toString());
          print("Product Date: " + products[index]['date'].toString());
          print("Selected Date: " + selectedDate.toString());
          if(DateFormat.yMMMd().format(products[index]['date']) ==
                DateFormat.yMMMd().format(selectedDate)) {
            return ProductCard(products[index], index, updateProduct);
          }
        }
      );
    }
    return ProductCard;
  }

  @override
  Widget build(BuildContext context) {
    return _buildCard();
  }
}

代码的问题是,如果我为新产品(即第三种产品)选择了不同的日期,则列表视图生成器不会将其带入循环中,甚至当我将新日期作为传递的日期数时,更糟循环中的产品减少1。请在下面找到打印声明以供参考(代码中的一个)

以默认日期打开页面时:

I/flutter (11152): Product List: [{name: Product 1, amount: 24.5, date: 2019-07-14 00:00:00.000}, {name: Product 2, amount: 29.5, date: 2019-07-14 00:00:00.000}, {name: Product 3, amount: 29.5, date: 2019-07-15 00:00:00.000}]
I/flutter (11152): Product Name: Product 1
I/flutter (11152): Product Date: 2019-07-14 20:13:05.838550
I/flutter (11152): Selected Date: 2019-07-14 20:15:32.687104
I/flutter (11152): Product List: //same list as above
I/flutter (11152): Product Name: Product 2
I/flutter (11152): Product Date: 2019-07-14 20:13:32.398186
I/flutter (11152): Selected Date: 2019-07-14 20:15:32.687104
I/flutter (11152): Product List: //same list as above
I/flutter (11152): Product Name: Product 3
I/flutter (11152): Product Date: 2019-07-15 00:00:00.000
I/flutter (11152): Selected Date: 2019-07-14 20:15:32.687104

当我选择其他日期(即2019-07-15)时:

I/flutter (11152): Product List: [{name: Product 1, amount: 24.5, date: 2019-07-14 00:00:00.000}, {name: Product 2, amount: 29.5, date: 2019-07-14 00:00:00.000}, {name: Product 3, amount: 29.5, date: 2019-07-15 00:00:00.000}]
I/flutter (11152): Product Name: Product 1
I/flutter (11152): Product Date: 2019-07-14 20:13:05.838550
I/flutter (11152): Selected Date: 2019-07-15 12:00:00.000Z
I/flutter (11152): Product List: //same list as above
I/flutter (11152): Product Name: Product 2
I/flutter (11152): Product Date: 2019-07-14 20:13:32.398186
I/flutter (11152): Selected Date: 2019-07-15 12:00:00.000Z7104

当选择日期时,我只想渲染具有与所选日期相同的日期的产品(卡片)。请帮助我找到实施中的错误,或者提出实现目标的更好方法。任何帮助表示赞赏,谢谢:)

1 个答案:

答案 0 :(得分:0)

好吧,我认为这会帮助您,而不是将其转换为字符串,然后使用7 <-> 4 <-> 5 <-> 4 类中提供的print(list.head!.nextNode!) 方法进行比较。您也可以使用7 <-> 4 <-> 5 属性来获取天数差异

代码:

difference()