Reputation: 439
In my app I need to preview a table which appear if the user entered some data. So I added a if statement. There is a button to clear the form too. My TextFormField is placed under the table. It won't get disposed until I clear the form. But after I clear the form it get disposed. I added another TextFormField beneath the table to check whether the table is causing the error. That TextFormField also got disposed. Why is this happening?
// This is the code of the Table
if (_data.length != 0)
Row(children: [
Expanded(
child: Padding(
padding: EdgeInsets.only(top: 15, bottom: 10),
child: DataTable(
columnSpacing: 20,
columns: const <DataColumn>[
DataColumn(label: Center(child: Text('Medications'))),
DataColumn(label: Center(child: Text('Amount'))),
DataColumn(label: Center(child: Text('Days'))),
DataColumn(label: Center(child: Text('When'))),
DataColumn(
label: Text(
'-',
style: TextStyle(fontSize: 22),
textAlign: TextAlign.center,
))
],
rows: _data
.map(
((element) => DataRow(
cells: [
(DataCell(Text(element["drug"]))),
DataCell(Text(element["amount"].toString(),
textAlign: TextAlign.center)),
DataCell(Text(element["days"].toString(),
textAlign: TextAlign.center)),
DataCell(Text(element["when"],
textAlign: TextAlign.center)),
DataCell(RButton(
onPressed: onPressedDrugs,
text: element["drug"],
))
],
)),
)
.toList(),
)))
]),
// This is the code of the TextFormField
Padding(
padding: EdgeInsets.symmetric(horizontal: 12, vertical: 10),
child: Row(
children: [
Expanded(
child: TextFieldSearch(
controller: controllerInvestigation,
initialList: allInvestigations,
label: "Enter the Investigation",
decoration: InputDecoration(
focusColor: Colors.purple,
border: OutlineInputBorder(),
labelText: "Enter the Investigation",
icon: FaIcon(FontAwesomeIcons.vial),
),
),
),
PopupMenuButton<String>(
elevation: 16,
icon: Icon(Icons.arrow_drop_down),
itemBuilder: (context) =>
List<String>.from(starInvestigations)
.map<PopupMenuItem<String>>((String value) {
return PopupMenuItem<String>(
value: value,
child: Container(
color: Colors.transparent,
width: double.infinity,
child: TextButton(
child: ListTile(
title: Text(value,
textAlign: TextAlign.left)),
onPressed: () {
setState(() {
controllerInvestigation.text = value;
});
Navigator.pop(context);
},
)));
}).toList()),
],
),
),
// This is the code of the Clear button
Padding(
padding: EdgeInsets.symmetric(
horizontal:
(MediaQuery.of(context).size.width / 2 - 150) / 2,
vertical: 10),
child: SizedBox(
width: 150,
child: FloatingActionButton.extended(
onPressed: () {
controllerName.text = '';
controllerAmount.text = '';
controllerDrug.text = '';
controllerAgeYears.text = '';
controllerDays.text = '';
controllerAgeMonths.text = '';
controllerInvestigation.text = '';
controllerSystolicPressure.text = '';
controllerDiastolicPressure.text = '';
controllerComplain.text = '';
_isSelected = [false, false];
setState(() {
_data = [];
selectedInvestigations = [];
getData();
});
},
focusColor: Colors.cyan[400],
label: const Text('Clear '),
icon: const Icon(Icons.clear_all),
backgroundColor: Colors.cyan[300],
),
)),
Upvotes: 0
Views: 176
Reputation: 439
The problem was that the TextEditingController get disposed after I Clear the form. So redefining the TextEditingController is the key to solve this problem. So I redefined the variable as shown below.
controllerInvestigation = TextEditingController();
Upvotes: 1