pass variables by parameters to be modified within the function

Issue

I have a method that takes care of the logic of saving the values of the TextFormField fields in variables, of different Forms, but they do not save the value in the String variables that I pass through parameters, when the Forms are validated.

This is the declaration of my variables from one of the forms:

    final formKeyMotherboard = new GlobalKey<FormState>();
  String _numInvMotherboard,
      _marcaMotherboard,
      _modeloMotherboard,
      _tipoMotherboard,
      _detallesMotherboard,
      _fechaMotherboard;

  final TextEditingController controllerNumInventarioMotherboard =
      new TextEditingController();
  final TextEditingController controllerMarcaMotherboard =
      new TextEditingController();
  final TextEditingController controllerModeloMotherboard =
      new TextEditingController();
  final TextEditingController controllerTipoMotherboard =
      new TextEditingController();
  final TextEditingController controllerDetallesMotherboard =
      new TextEditingController();
  final TextEditingController controllerFechaMotherboard =
      new TextEditingController();

This is the method:

    Step _defaulFrom(
      {@required IconData icon,
      @required String nameForm,
      @required Key keyForm,
      @required String saveNumInv,
      @required String saveMarca,
      @required String saveModelo,
      @required String saveTipo,
      @required String saveDetalles,
      @required String saveFecha,
      @required TextEditingController controllerNumInv,
      @required TextEditingController controllerMarca,
      @required TextEditingController controllerModelo,
      @required TextEditingController controllerTipo,
      @required TextEditingController controllerDetalle,
      @required TextEditingController controllerFecha,
      TextEditingController controllerEncargado,
      String saveEncargado}) {
    return Step(
        title: Text(
          nameForm,
          style: TextStyle(
            color: Colors.black45,
            fontSize: 22.0,
            fontWeight: FontWeight.bold,
          ),
        ),
        content: Form(
            key: keyForm,
            child: Padding(
              padding: const EdgeInsets.all(12.0),
              child: Column(
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.only(top: 8.0),
                    child: TextFormField(
                      controller: controllerNumInv,
                      autocorrect: false,
                      decoration: _defaulImputDecoration(NUM_INVENTARIO, icon),
                      validator: (val) =>
                          val.isEmpty ? ERROR_RELLENE_CAMPO : null,
                      `enter code here`onSaved: (value) => saveNumInv = value,

                      keyboardType: TextInputType.numberWithOptions(),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(top: 8.0),
                    child: TextFormField(
                      controller: controllerMarca,
                      autocorrect: false,
                      decoration: _defaulImputDecoration(MARCA, icon),
                      validator: (val) =>
                          val.isEmpty ? ERROR_RELLENE_CAMPO : null,
                      onSaved: (value) => saveMarca = value,

                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(top: 8.0),
                    child: TextFormField(
                      controller: controllerModelo,
                      autocorrect: false,
                      decoration: _defaulImputDecoration(MODELO, icon),
                      validator: (val) =>
                          val.isEmpty ? ERROR_RELLENE_CAMPO : null,
                      onSaved: (value) => saveModelo = value,

                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(top: 8.0),
                    child: TextFormField(
                      controller: controllerTipo,
                      autocorrect: false,
                      decoration: _defaulImputDecoration(TIPO, icon),
                      validator: (val) =>
                          val.isEmpty ? ERROR_RELLENE_CAMPO : null,
                      onSaved: (value) => saveTipo = value,

                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(top: 8.0),
                    child: TextFormField(
                      controller: controllerDetalle,
                      autocorrect: false,
                      decoration: _defaulImputDecoration(DETALLES, icon),
                      validator: (val) =>
                          val.isEmpty ? ERROR_RELLENE_CAMPO : null,
                      onSaved: (value) => saveDetalles = value,

                    ),
                  ),
                  controllerEncargado != null
                      ? Padding(
                          padding: const EdgeInsets.only(top: 8.0),
                          child: TextFormField(
                            controller: controllerEncargado,
                            autocorrect: false,
                            decoration: _defaulImputDecoration(
                                NOMBRE_ENCARGADO, Icons.person),
                            validator: (val) =>
                                val.isEmpty ? ERROR_RELLENE_CAMPO : null,
                            onSaved: (value) => saveEncargado = value,

                          ),
                        )
                      : Divider(
                          color: Colors.transparent,
                        ),
                  Padding(
                    padding: const EdgeInsets.only(top: 8.0),
                    child: TextFormField(
                      autocorrect: false,
                      controller: controllerFecha,
                      decoration: InputDecoration(
                        suffixIcon: IconButton(
                            icon: Icon(Icons.date_range),
                            onPressed: () {
                              _selectDate(context);
                              setState(() {
                                //_saveDate = new DateFormat.yMd(_dateTime)
                              });
                            }),
                        contentPadding: EdgeInsets.all(16.0),
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(25.0),
                        ),
                        labelText: 'Fecha',
                        labelStyle: TextStyle(
                            color: Colors.black54,
                            fontSize: 16.0,
                            decorationStyle: TextDecorationStyle.wavy),
                      ),
                      validator: (val) =>
                          val.isEmpty ? ERROR_RELLENE_CAMPO : null,
                      onSaved: (value) => saveFecha = value,

                    ),
                  ),

               }

                  })
                ],
              ),
            )));
  }

With this method I want to reduce code, I have 5 forms that have the same code, the only thing that changes are the values where the values of the TextFormField obtained from the TextEditingController with controller.text are saved and saved, it tells me that its values are null

Solution

Dart does not have “reference parameters” or “inout” parameters. Arguments are passed as values, so there is no way for the function to change the variable that that value came from.

If you need something changed, you can either create a mutable object containing the state to modify, and pass that to the function, or you can ensure that the function can see the variables directly (which is usually not possible if the function is used from multiple locations).

Answered By – lrn

Answer Checked By – Jay B. (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.