0
votes

I have a only one question

I begin learn a flutter in yesterday.

How to get a Textfield value ?

I tried call a TextEditingController anybody in a class. But It's not working to.

[![enter image description here][1]][1]

class LoginScreen extends StatefulWidget {
  @override
  _LoginState createState() => _LoginState();
}

class _LoginState extends State<LoginScreen> {
  final userId = TextEditingController();
  final userPass = TextEditingController();

  ...TextInputField(),
          PasswordInput(),
          LoginButton()

}

void loginAct() async {
  var data = {
    "user_id": userId,   //undefined
    "user_pass": userPass,  //undefined
  };



class TextInputField extends StatelessWidget {
  const TextInputField({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 10.0),
      child: Container(
        height: size.height * 0.08,
        width: size.width * 0.8,
        decoration: BoxDecoration(
          color: Colors.grey[500],
          borderRadius: BorderRadius.circular(16),
        ),
        child: TextField(
          controller: userId,  <=== (The getter 'userId' isn't defined for the class 'TextInputField')
          decoration: InputDecoration(
            border: InputBorder.none,
            prefixIcon: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 20.0),
              child: Icon(
                FontAwesomeIcons.user,
                size: 28,
                color: Colors.black54,
              ),
            ),
            hintText: 'id',
          ),
        ),
      ),
    );
  }
}

   

how to resolve that?

2
you want to login and pass your data to API it is correct?Ravindra S. Patil

2 Answers

3
votes

if flutter you can get text field value by different methods

  1. by text editing controller

add this on top

final userNameController = TextEditingController();
final passwordController = TextEditingController();

then on text field

TextField(
  controller: userNameController,
);

 TextField(
  controller: passwordController,
);

and on function pass the value as you like

onPressed: () => loginAct(userNameController.text,passwordController.text),

in this case you need to specify what is initial value through controller

for Second Question

http.get(Uri.https('https://swapi.co', 'api/people'));

or

http.get(Uri.parse('https://swapi.co/api/people'));
1
votes

try this on your code

class TextInputField  extends StatelessWidget {
  const TextInputField({ Key? key }) : super(key: key);
@override
Widget build(BuildContext context) {
  final userId = TextEditingController();
  final userPass = TextEditingController();
       return Container(
          child: TextField(
         controller: userId,  
      )
   );
  }
}