I am not able to initilize controller memeber in flutter, how can i fix it

39 Views Asked by At

import 'package:flutter/material.dart';

class Test extends StatelessWidget {
  
  final controller;
  final String hintText;
  final bool obscureText;

  const Test({
    Key? key,
    required controller,
    required this.hintText,
    required this.obscureText,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final formFieldKey = GlobalKey<FormFieldState<String>>();

    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 25.0),
      child: Column(
        children: [
          TextFormField(
            key: formFieldKey,
            controller: controller,
            obscureText: obscureText,
            decoration: InputDecoration(
              enabledBorder: const OutlineInputBorder(
                borderSide: BorderSide(color: Colors.orange),
              ),
              focusedBorder: const OutlineInputBorder(
                borderSide: BorderSide(color: Colors.white),
              ),
              fillColor: Colors.red,
              filled: true,
              labelText: hintText,
              // Add any other desired properties
            ),
            validator: (value) {
              if (value == null || value.isEmpty) {
                return 'This field is required';
              }
              return null;
            },
          ),
          ElevatedButton(
            onPressed: () {
              // Validate the field
              formFieldKey.currentState?.validate();
            },
            child: const Text('Submit'),
          ),
        ],
      ),
    );
  }
}

I am trying to use controller member but can't, I'll try , var , text editingcontroller but still facing same error,

One most , I don't know why my complier not avoiding this type of error to execute my code , Thanks, hope you answer me as soon as possible.

1

There are 1 best solutions below

0
nebiberke On
import 'package:flutter/material.dart';

class Test extends StatelessWidget {
  const Test({
    required this.controller,
    required this.hintText,
    required this.obscureText,
    super.key,
  });
  final TextEditingController controller;
  final String hintText;
  final bool obscureText;

  @override
  Widget build(BuildContext context) {
    final formFieldKey = GlobalKey<FormFieldState<String>>();

    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 25),
      child: Column(
        children: [
          TextFormField(
            key: formFieldKey,
            controller: controller,
            obscureText: obscureText,
            decoration: InputDecoration(
              enabledBorder: const OutlineInputBorder(
                borderSide: BorderSide(color: Colors.orange),
              ),
              focusedBorder: const OutlineInputBorder(
                borderSide: BorderSide(color: Colors.white),
              ),
              fillColor: Colors.red,
              filled: true,
              labelText: hintText,
              // Add any other desired properties
            ),
            validator: (value) {
              if (value == null || value.isEmpty) {
                return 'This field is required';
              }
              return null;
            },
          ),
          ElevatedButton(
            onPressed: () {
              // Validate the field
              formFieldKey.currentState?.validate();
            },
            child: const Text('Submit'),
          ),
        ],
      ),
    );
  }
}

You did not specify the controller type as TextEditingController and you defined it incorrectly in the constructor method.