I want to add textfield that has its own unique index. So I declared a list of TextFieldEditingController and call it in the Wrap(). controllers.add(TextEditingController()),
. But I am getting this error while calling it. I am getting this error This expression has a type of 'void' so its value can't be used. Try checking to see if you're using the correct API; there might be a function or call that returns void you didn't expect. Also check type parameters and variables which might also be void.
.
List<TextEditingController> controllers = [];
Wrap(
children: retrieveData.map((order) {
return Row(
children: [
Expanded(
child: Card(
child:
SvgPicture.asset('assets/toiletpaper.svg')),
),
Expanded(
flex: 2,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
CustomText(
text: order.commodityName.toString()),
CustomText(text: '60 rolls per pack'),
SizedBox(
height: screenHeight(context) / 40,
),
controllers.add(TextEditingController()), // this is the error
Flexible(
child: TextFormValidator(
fillColor: Colors.white,
contentPadding:
EdgeInsets.symmetric(vertical: 8),
outlineInputBorder: OutlineInputBorder(),
controller: description,
textAlign: TextAlign.left),
)
],
),
),
)
],
);
}).toList()),
2
Answers
The line causing the error is just an expression:
controllers.add(...)
returnsvoid
. You placed that line inside of thechildren: [...]
list, which has the typeList<Widget>
.That’s why this doesn’t work:
I don’t understand entirely what you are trying to achieve, that’s why I can’t give you a working solution, but maybe this will work:
the
children
property of theColumn
widget is just aList<Widget>
, so it accepts only the actual flutter widget to be set inside of it, in your code, by doing the:you’re trying just to put another type than what is accepted from the
children
property, theadd()
method do just add elements to aList
which is thecontrollers
list and doesn’t really return aWidget
, it has a type ofvoid
, so the error is thrown.you will need to take it and put it in a scope that accepts calling method (
add()
), likebuild()
function scope, a lifecycle method suchinitState()
, or even wrapping a widget with aBuilder
widget then call it inside thebuilder
property.. :