Search code examples
flutterdartflutter-textinputfield

My textfield is returning null, so somehow the value isn't being passed from the onChanged -> the variable


Here is the bulk of the code:

I have tried modifying how I pass though the variable, on my other projects a simple function passthrough was sufficient but I must be initializing it incorrectly.. If I leave it as a late String Password then I receive an error that it was never initialized. I expect to see the password printed in the console although now it is showing null.``

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

late String email;
String? password; 
late String firstName;
late String lastName;

class RegistrationScreen extends StatefulWidget {
  static const String id = 'registration_screen';
  @override
  State<RegistrationScreen> createState() => _RegistrationScreenState();
}

class _RegistrationScreenState extends State<RegistrationScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: AnnotatedRegion<SystemUiOverlayStyle>(
        value: SystemUiOverlayStyle.light,
        child: GestureDetector(
          onTap: () => FocusScope.of(context).unfocus(),
          child: Stack(
            children: [
              Container(
                height: double.infinity,
                child: SingleChildScrollView(
                  physics: const AlwaysScrollableScrollPhysics(),
                  padding: const EdgeInsets.symmetric(
                    horizontal: 40.0,
                    vertical: 120.0,
                  ),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Column(
                        children: [
                          SuildTextField(
                            'Password',
                            'Enter your password',
                            Icons.key_outlined,
                            true,
                            TextInputType.text,
                                (value) {
                              password = value;
                            },
                          ),
                          SizedBox(height: 10.0),
                          _BuildCreateAccountButton(),
                        ],
                      )
                    ],
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

class _BuildCreateAccountButton extends StatefulWidget {
  const _BuildCreateAccountButton();

  @override
  State<_BuildCreateAccountButton> createState() =>
      _BuildCreateAccountButtonState();
}

class _BuildCreateAccountButtonState extends State<_BuildCreateAccountButton> {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 25.0),
      width: double.infinity,
      child: ElevatedButton(
        onPressed: () {
          setState(() {
            print(
                'Create Account Button was pressed. print: $password'); //TODO Link registration_screen: Create Account button
          });
        },
        child: Row(
          children: const [
            Material(
              child: Text(
                'SIGN UP ',
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class SuildTextField extends StatelessWidget {
  SuildTextField(this.titleLine, this.textField, this.fieldIcon, this.isSecure,
      this.textInputType, this.userTyped,
      {super.key});
  final String titleLine;
  final String textField;
  final IconData fieldIcon;
  final bool isSecure;
  final TextInputType textInputType;
  final Function userTyped;

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(titleLine, style: kLabelStyle),
        const SizedBox(height: 10.0),
        Container(
          child: TextField(
            obscureText: isSecure,
            keyboardType: textInputType,
            onChanged: (value) => userTyped,
            onSubmitted: (value) {},
            style: const TextStyle(color: Colors.white),
            decoration: InputDecoration(
              border: InputBorder.none,
              contentPadding: const EdgeInsets.only(top: 14.0),
              prefixIcon: Icon(
                fieldIcon,
                color: Colors.white,
              ),
              hintText: textField,
              hintStyle: kHintTextStyle,
            ),
          ),
        ),
      ],
    );
  }
}`


Solution

  • this should work:

    onChanged:(value) => userTyped(value)