Home > front end >  TextEditingController returns me null , not considered the input I typed inside text field
TextEditingController returns me null , not considered the input I typed inside text field

Time:09-22

questionController.text always returns me a null value. rather than Whatever I insert in textformfield.

class _AddQuestionState extends State<AddQuestion> {

  TextEditingController questionController = TextEditingController();
  
   
  @override
  Widget build(BuildContext context) {
    

    return Scaffold(
      body:  Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            TextFormField(
              controller: questionController,
              decoration: InputDecoration(
                hintText: 'description',
                hintStyle: TextStyle(
                    color: Colors.grey
                ),
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.all(Radius.circular(20.0)),
                ),
              ),),
             AddUser(questionController.text),]

}}

When I called this class and print the output, it returns me null.

Using AddUser(questionController.text);

I printed the output using print(question) but it returns me empty string.

class AddUser extends StatelessWidget {
  final String question;

  
  AddUser(this.question);
  
  @override
  Widget build(BuildContext context) {
    // Create a CollectionReference called users that references the firestore collection
    CollectionReference users = FirebaseFirestore.instance.collection('Questions').doc("cse").collection("CSE");

    Future<void> addUser() {
      print(question);
      // Call the user's CollectionReference to add a new user
      return users
          .add({
        'question': question, // John Doe
        
      })
          .then((value) => print("User Added"))
          .catchError((error) => print("Failed to add user: $error"));
    }

CodePudding user response:

What happen is when it first build the screen your AddQuestion widget questionController is set to empty string which is passed to the AddUser() widget.

If [controller] is null, then a [TextEditingController] will be constructed automatically and its text will be initialized to [initialValue] or the empty string.

When you changed the value in your questionController, your AddUser() widget didnt know the changes. By adding setState it will rebuild the whole AddQuestion widget and passed the new value to your AddUser widget.

Try this example to have an undestanding

class MyWidget extends StatefulWidget {
  const MyWidget({Key? key}) : super(key: key);

  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  TextEditingController questionController = TextEditingController();
  String? questionValue;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          children: [
            TextFormField(
              controller: questionController,
              decoration: InputDecoration(
                hintText: 'description',
                hintStyle: TextStyle(color: Colors.grey),
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.all(Radius.circular(20.0)),
                ),
              ),
            ),
            SizedBox(height: 20),
            TextButton(
              onPressed: () {
                print("questionController: ${questionController.text}");
                setState(() {
                  questionValue = questionController.text;
                });
              },
              child: Text("SETSTATE BUTTON"),
            ),
            SizedBox(height: 20),
            Text("questionValue: $questionValue"),
            SizedBox(height: 20),
            AddUser(questionController.text),
          ],
        ),
      ),
    );
  }
}

class AddUser extends StatelessWidget {
  final String question;

  AddUser(this.question);

  @override
  Widget build(BuildContext context) {
    return Text("question: $question");
  }
}
  • Related