类型 '(DateTime) => String' 不是类型转换中类型 'DateTime' 的子类型

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

我正在创建一个洗衣应用程序,但我看到此错误说不能在字符串中使用数据时间。我提供了所有涉及文本中包含日期时间的代码,并提供了运行错误。这是在字符串中使用日期时间的 2 个类的附加代码,我仍然面临这个问题请帮助 [[[[[enter image description here](https://i.stack.imgur.com/04mlo.png)](https://i.stack.imgur.com/HFdNH.png)](https://i.stack.imgur.com/wGxNi.png)]( https://i.stack.imgur.com/4QBwk.png)](https://i.stack.imgur.com/cauzQ.png

`import 'package:flutter/material.dart';
import 'package:intl/intl.dart'; // Import DateFormat for date formatting
import 'package:get/get.dart';
import 'package:laundry_app/config/app_format.dart'; // Assuming AppFormat contains date and currency formatting methods
import 'package:laundry_app/data/model/laundry.dart'; // Assuming Laundry model is defined in this file

class DetailLaundry extends StatelessWidget {
  const DetailLaundry({Key? key, required this.laundry}) : super(key: key);
  final Laundry laundry;

  @override
  Widget build(BuildContext context) {
    return ListView(
      padding: EdgeInsets.all(5.0),
      children: [
        dataText('Customer', laundry.customerName!),
        dataText('Status', laundry.status!),
        dataText('Queue Date', AppFormat.date(laundry.queueDate!)),
        dataText('Weight', '${laundry.weight} Kg'),
        dataText('Price', AppFormat.currency(laundry.price!)),
        SizedBox(height: 10),
        if (laundry.startDate != null || laundry.endDate != null) ...[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              if (laundry.startDate != null)
                Padding(
                  padding: EdgeInsets.only(bottom: 5.0),
                  child: Text('Washing (Start)'),
                ),
              if (laundry.endDate != null)
                Padding(
                  padding: EdgeInsets.only(bottom: 5.0),
                  child: Text('Done (End)'),
                ),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              if (laundry.startDate != null)
                Text(
                  laundry.startDate != null
                      ? DateFormat('yyyy-MM-dd').format(laundry.startDate!)
                      : 'No Date',
                  textAlign: TextAlign.left,
                  style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18.0),
                ),

              if (laundry.endDate != null)
                Text(
                  laundry.endDate != null
                      ? DateFormat('yyyy-MM-dd').format(laundry.endDate!)
                      : 'No Date',
                  textAlign: TextAlign.left,
                  style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18.0),
                ),
            ],
          ),
        ],
      ],
    );
  }

  Widget dataText(String title, String body) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Text(title),
        SizedBox(height: 5), // Use SizedBox for spacing
        Text(
          body,
          style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18.0),
        ),
        SizedBox(height: 5), // Use SizedBox for spacing
      ],
    );
  }
}

class WhereStatusPage extends StatefulWidget {
  const WhereStatusPage({Key? key, required this.status}) : super(key: key);
  final String status;
  
  @override
  State<WhereStatusPage> createState() => _WhereStatusPageState();
}

class _WhereStatusPageState extends State<WhereStatusPage> {
  final cWhereStatus = Get.put(CWhereStatus());

  void refresh() {
    cWhereStatus.setList(widget.status);
  }

  // Calling the function with a DateTime object

  @override
  void initState() {
    // TODO: implement initState
    refresh();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: DView.appBarLeft(widget.status),
      body: GetBuilder<CWhereStatus>(
        builder: (_) {
          if (_.list.isEmpty) {
            return DView.empty();
          }

          return ListView.builder(
              itemCount: _.list.length,
              itemBuilder: (context, index) {
                Laundry laundry = _.list[index];
                return Card(
                  child: ListTile(
                    onTap: () {
                      Get.to(() => DetailPage(laundry: laundry))?.then((value) {
                        if (value ?? false) {
                          refresh();
                        }
                      });
                    },
                    leading: CircleAvatar(
                      radius: 18.0,
                      child: Text('${index + 1}'),
                    ),
                    horizontalTitleGap: 0,
                    title: Row(
                      children: [
                        Text(laundry.customerName!),
                        SizedBox(width: 5),
                        Text(
                          '${laundry.id}',
                          style: TextStyle(fontSize: 13.0, color: Colors.grey),
                        ),
                      ],
                    ),
                    subtitle: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          laundry.queueDate != null
                              ? DateFormat('yyyy-MM-dd')
                                  .format(laundry.queueDate!)
                              : 'No Date',
                        ),
                        Text(
                          laundry.queueDate != null
                              ? DateFormat('yyyy-MM-dd')
                                  .format(laundry.queueDate!)
                              : 'No Date',
                        ),
                      ],
                    ),
                    trailing: Icon(Icons.navigate_next),
                  ),
                );
              });
        },
      ),
    );
  }
}`


flutter datetime flutter-text flutter-datetime-picker
1个回答
0
投票

您的 laundry.queueDate 数据是 String 类型,您可以转换为 dateTime 类型

问题就会解决!

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