khomotjo_codes
khomotjo_codes

Reputation: 137

Undefined names

I have generated or declared ,whatever term is correct to use, the controller and hintText from the constructor but when I use them I get an error saying undefined " controller" try correcting the name to the one defined or defining it. The same goes for hintText. Here's my code below

import 'package:flutter/material.dart';

class CustomTextField extends StatefulWidget {

  final TextEditingController controller;
  final String hintText;

  const CustomTextField({
Key? key, 
required this.controller,
required this.hintText

}): super(key: key);

  @override
  State<CustomTextField> createState() => _CustomTextFieldState();
}

class _CustomTextFieldState extends State<CustomTextField> {
  @override
  Widget build(BuildContext context) {
    return TextFormField(
      controller: controller,
      decoration: const InputDecoration(
        hintText: hintText,
        border: OutlineInputBorder(
            borderSide: BorderSide(
          color: Colors.black26,
        )),
        enabledBorder: OutlineInputBorder(
          borderSide: BorderSide(
            color: Colors.black26,
          ),
        ),
      ),
      validator: (val) {},
    );
  }
}

Upvotes: 0

Views: 297

Answers (1)

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63749

You are using StatefulWidget. To use variable from StatefulWidget to its State class (here in _CustomTextFieldState), you need to use

widget.variableName

For your case it will be

return TextFormField(
  controller: widget.controller,
  decoration:   InputDecoration(
    hintText: widget.hintText,
    border: OutlineInputBorder(
        borderSide: BorderSide(
      color: Colors.black26,
    )),

More about StatefulWidget.

Upvotes: 1

Related Questions