Reputation: 1
i am creating a laundry app but i am seeing this error saying cant use datatime in string. i have provided every code which have datetime in text involved and provided run error. here is the additional code of 2 classes which uses datetime in string, still im facing this issue please help [[[[[enter image description here](https://i.sstatic.net/04mlo.png)](https://i.sstatic.net/HFdNH.png)](https://i.sstatic.net/wGxNi.png)](https://i.sstatic.net/4QBwk.png)](https://i.sstatic.net/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),
),
);
});
},
),
);
}
}`
> import 'package:intl/intl.dart';
class AppFormat {
static String date(DateTime dateTime) {
return DateFormat('EEEE , d MMM yyyy').format(date as DateTime);
}
static String time(DateTime dateTime) {
return DateFormat('HH:mm').format(dateTime);
}
static String currency(double price) {
return NumberFormat.currency(
decimalDigits: 2,
symbol: '\$',
locale: 'en_US',
).format(price);
}
}
Upvotes: 0
Views: 50