未处理的异常:“IconData”类型不是“String”类型的子类型

时间:2021-02-01 04:38:34

标签: flutter dart

我正在尝试在表格的最后一列中添加一个图标,以便它出现在 Pdf UI 中。我使用的依赖是 pdf: ^2.0.0, flutter_full_pdf_viewer: ^1.0.6, Printing: ^4.0.0 但我得到了一个错误说明如Unhandled Exception: type 'IconData' is not a subtype of type 'String'。我可以知道我正在做的错误是什么,因为我没有将图标声明为字符串,所以我可以知道这里出了什么问题吗?

class Product {
  const Product(this.serial,
      this.articleNumber,
      this.addresseeName,
      this.remark,
      this.image
      );

  final String serial;
  final String articleNumber;
  final String addresseeName;
  final String remark;
  final IconData image;


  getIndex(int index) {
    switch (index) {
      case 0:
        return serial;
      case 1:
        return articleNumber;
      case 2:
        return addresseeName;
      case 3:
        return remark.toString();
      case 4:
        return image;
    }
    return '';
  }

}
final products = <Product>[
    Product('1', 'RK1234567890IN', '', 'NOT DELIVERED', Icons.ac_unit),
    Product('2', 'RM0987654321IN', 'Ramesh', 'DELIVERED \n ( To : name)\n29-01-2021 10:03:43', Icons.ac_unit),
    Product('3', 'RL4345424741IN', 'Suresh', 'DELIVERED \n ( To : name)\n29-01-2021 10:30:13', Icons.ac_unit),
    Product('4', 'RO9638642753IN', '', 'NOT DELIVERED', Icons.ac_unit),
  ];

这就是我声明表格的方式

  pw.Widget _contentArticleTable(pw.Context context) {
    const tableHeaders = [
      'SERIAL',
      'ARTICLE NUMBER',
      'ADDRESSEE NAME',
      'REMARK',
      'SIGNATURE'
    ];
    return pw.Table.fromTextArray(
      cellAlignment: pw.Alignment.centerLeft,
      headerDecoration: pw.BoxDecoration(
        borderRadius: const pw.BorderRadius.all(pw.Radius.circular(2)),
        color: PdfColors.grey200,
      ),
      headerHeight: 25,
      cellHeight: 40,
      cellAlignments: {
        0: pw.Alignment.center,
        1: pw.Alignment.center,
        2: pw.Alignment.center,
        3: pw.Alignment.center,
        4: pw.Alignment.center,
      },
      headerStyle: pw.TextStyle(
        color: PdfColors.blueGrey,
        fontSize: 10,
        fontWeight: pw.FontWeight.bold,
      ),
      cellStyle: const pw.TextStyle(
        color: PdfColors.black,
        fontSize: 10,
      ),
      rowDecoration: pw.BoxDecoration(
        border: pw.Border(
          bottom: pw.BorderSide(
            color: PdfColors.amber,
            width: .5,
          ),
        ),
      ),
      headers: List<String>.generate(
        tableHeaders.length,
            (col) => tableHeaders[col],
      ),
      data: List<List<String>>.generate(
        products.length,
            (row) => List<String>.generate(
          tableHeaders.length,
              (col) => products[row].getIndex(col),
        ),
      ),
    );
  }

2 个答案:

答案 0 :(得分:0)

您不应将 IconData 作为字符串传递。您可能将其传递为

child: Icon(
    "widget.icon", //remove the quotes or any string interpolation
     size: 16.0,
  ),

这是使用图标的正确方法。

child: Icon(
    widget.icon,
     size: 16.0,
  ),

答案 1 :(得分:0)

这可能是错误

  getIndex(int index) {
    switch (index) {
      case 0:
        return serial;
      case 1:
        return articleNumber;
      case 2:
        return addresseeName;
      case 3:
        return remark.toString();
      case 4:
        return image; //this is fine if the condition is true but below might cause and error
    }
    return ''; //this causes the error during dart compilation on the condition this return a string value
  }