首页 > 解决方案 > Flutter 在初始化期间读取静态变量

问题描述

我有一个布尔值要从另一个屏幕中的复选框传递,它是来自SubmitForm()类的valueToSend 。我怎样才能访问该变量,以便错误消失并且该可以用作部分参数。错误是在初始化期间读取静态变量

var value = SubmitForm().valueToSend; //here is the error come 

final header = ['Parts', 'Check'];

final parts = [
  Part(parts: 'Conveyor Belt', check: value ? 'done' : 'have issue'),];
     
    
class SubmitForm extends StatelessWidget {      

final bool valueToSend;

final data = parts.map((Part) => [Part.parts, Part.check]).toList();
}

标签: flutterstatic

解决方案


我终于设法得到它。

class SubmitForm extends StatelessWidget {


final String text;
final String taskName;
final currentDate;
final dropdownValue;
final bool valueToSend;

final pdf = pw.Document();

  writeOnPdf() {
    final parts = [
      Part(parts: 'Conveyor Belt', check: valueToSend ? 'done' : 'have issue'), //i got it now
//      Part(parts: 'Gear Motor', check: 'done'),
//      Part(parts: 'Pulley&Roller', check: 'done'),
//      Part(parts: 'Side Frame', check: 'done'),
//      Part(parts: 'Motor', check: 'done'),
//      Part(parts: 'DMS', check: 'done'),
//      Part(parts: 'Sensor', check: 'done'),
//      Part(parts: 'Beacon', check: 'done'),
//      Part(parts: 'E/stop', check: 'done'),
    ];
    final data = parts.map((Part) => [Part.parts, Part.check]).toList();

    pdf.addPage(pw.MultiPage(
        pageFormat: PdfPageFormat.a5,
        margin: pw.EdgeInsets.all(32),
        build: (pw.Context context) {
          return <pw.Widget>[
            pw.Header(level: 0, child: pw.Text('Date:$currentDate')),
            pw.Header(
                // show image from the asset image TODO provide image to pdf
                level: 1,
                child: pw.Text('Baggage Handling System T2, MA(SEPANG) SDN BHD',
                    style: pw.TextStyle(fontWeight: pw.FontWeight.bold))),
            pw.Header(level: 2, child: pw.Text('PPM Inspection Check Sheet')),
            pw.Paragraph(
                text: '$taskName Subsystem $text done by shift $dropdownValue'),
            pw.Table.fromTextArray(
              headers: header,
              data: data,
              headerDecoration: pw.BoxDecoration(
                color: PdfColors.grey300,
              ),
            ),
          ];
        }));
  }

推荐阅读