Home > Net >  how to get save the value from textfromfield and pass it back to page?
how to get save the value from textfromfield and pass it back to page?

Time:10-25

import 'package:flutter/material.dart';
import 'package:mmitra/widgets/header.dart';
import 'home.dart';

class CreateAccount extends StatefulWidget {
  
  @override
  _CreateAccountState createState() => _CreateAccountState();
}

class _CreateAccountState extends State<CreateAccount> {
  late String username;
  final _key = Global Key<FormState>();

  

  @override
  Widget build(BuildContext parentContext) {
    return Scaffold(
      appBar: header(context, titleText: 'Create Account'),
      body: ListView(children: [
        Container(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Padding(
                padding: EdgeInsets.only(top: 25),
                child: Center(
                  child: Text(
                    'Create a username',
                    style: TextStyle(fontSize: 25.0),
                  ),
                ),
              ),
              Padding(
                padding: EdgeInsets.all(16.0),
                child: Form(
                  child: TextFormField(
                    key: _key,
                    // controller: myController,
                    onSaved: (val) => username = val!,

                    decoration: InputDecoration(
                        border: OutlineInputBorder(),
                        labelStyle: TextStyle(fontSize: 15),
                        labelText: 'Username',
                        hintText: 'Must be at least 3 Characters'),
                  ),
                ),
              ),
              GestureDetector(
                onTap: () {
                  _key.currentState!.save();
                  Navigator.pop(context, username
                      );
                },
                child: Container(
                  height: 50,
                  width: 300,
                  decoration: BoxDecoration(
                      color: Colors.blue,
                      borderRadius: BorderRadius.circular(7)),
                  child: Center(
                    child: Text(
                      'Submit',
                      style:
                          TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
                    ),
                  ),
                ),
              ),
              
            ],
          ),
        ),
      ]),
    );
  }
}

and I'm getting below shown errors:

Exception caught by gesture  
The following Cast Error was thrown while handling a gesture  
Null check operator used on a null value

I just want to get the textfromfield value and i want to pass it to the home.dart page in order to create the document in firebase collection

CodePudding user response:

You added key in TextFormField

 Form(
              child: TextFormField(
                key: _key,
                // controller: myController,
                onSaved: (val) => username = val!,

                decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelStyle: TextStyle(fontSize: 15),
                    labelText: 'Username',
                    hintText: 'Must be at least 3 Characters'),
              ),
            ),
          ),

Remove it from TextFormField and Add it in Form

Just Like:

 Form(
           key: _key,
              child: TextFormField(
                
                // controller: myController,
                onSaved: (val) => username = val!,

                decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelStyle: TextStyle(fontSize: 15),
                    labelText: 'Username',
                    hintText: 'Must be at least 3 Characters'),
              ),
            ),
          ),

This will Solve your Issue.

CodePudding user response:

you must define a EditTextControll() ex:textController and set it to TextFormField to controller paramerter , and then get text as textController.text and pass with Navigatoer .

CodePudding user response:

for pass wihtin first screen to two screen

Firstly /

TextEditingController controller = TextEditingController();

Secoundly /

body: Form(
        child: TextFormField(
          controller: controller,
          onTap: (){
            //here SettingsScreen() is example
            // name is paramerter in the secound screen
            Navigator.push(context,
              MaterialPageRoute(builder: (context)=>SettingsScreen(name:controller.text),),
            );
          },
        ),
      ),
  • Related