'showDatePciker throwing an error in Flutter Project
I am trying to load Date from showDatePicker. But when i run the code I get the following error. Unable to understand where am I going wrong. Please help me This is the Error I am getting. Where should i use the future unable to understand. Can somebody tell me how can this error be resolved. Any help would be appreciated
type 'Future<dynamic>' is not a subtype of type '(() => void)?'
import 'package:flutter/material.dart';
class EditTask extends StatefulWidget {
const EditTask({
Key? key,
//this.taskModel,
}) : super(key: key);
@override
_EditTaskState createState() => _EditTaskState();
}
class _EditTaskState extends State<EditTask> {
late DateTime selectedDate;
@override
void initState() {
// TODO: implement initState
selectedDate = DateTime.now();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Create Task'),
),
body: Padding(
padding: const EdgeInsets.all(18.0),
child: Column(
children: [
Row(
children: [
Icon(Icons.calendar_today),
SizedBox(width: 10),
Text(
"${selectedDate.day}/${selectedDate.month}/${selectedDate.year}"),
SizedBox(width: 10),
IconButton(
onPressed: _selectDate(context), icon: Icon(Icons.edit)),
],
),
SizedBox(height: 16),
],
),
),
);
}
_selectDate(BuildContext context) async {
final DateTime? selected = await showDatePicker(
context: context,
initialDate: selectedDate,
firstDate: DateTime(2010),
lastDate: DateTime(2025),
);
if (selected != null && selected != selectedDate)
setState(() {
selectedDate = selected;
});
}
}
Solution 1:[1]
Change the onPressed function in IconButton
IconButton(
onPressed: () {
_selectDate(context);
},
icon: Icon(Icons.edit),
),
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Anushka Chauhan |
