Home > Enterprise >  Getting error on TextFieldEditingController
Getting error on TextFieldEditingController

Time:02-04

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()),

CodePudding user response:

The line causing the error is just an expression: controllers.add(...) returns void. You placed that line inside of the children: [...] list, which has the type List<Widget>.

That's why this doesn't work:

children: [CustomText(...), CustomText(...), controllers.add(...), Flexible(...)]
            ^ Widget         ^ Widget             ^ void             ^ Widget

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:

Wrap(
   children: retrieveData.map((order) {
       controllers.add(TextEditingController())   // move the line here
       return Row(
             children: [
                 ...
             ]
       );
    }).toList(),
)

CodePudding user response:

the children property of the Column widget is just a List<Widget>, so it accepts only the actual flutter widget to be set inside of it, in your code, by doing the:

 SizedBox(
   height: screenHeight(context) / 40,
    ),
 controllers.add(TextEditingController()),
 //...

you're trying just to put another type than what is accepted from the children property, the add() method do just add elements to a List which is the controllers list and doesn't really return a Widget, it has a type of void, so the error is thrown.

you will need to take it and put it in a scope that accepts calling method (add()), like build() function scope, a lifecycle method such initState(), or even wrapping a widget with a Builder widget then call it inside the builder property.. :

Widget build() {
 controllers.add(TextEditingController()),

// Your widget here
}
  • Related