Flutter: Getting Value From TextField from class A and Pass it To Another class B

Viewed 739

I have developed an app in which users will enter text in text field. These text fields are located under the class HomeScreen(homescreen.dart);

            Padding(
            padding: const EdgeInsets.all(8.0),
            child: TextField(
              decoration: InputDecoration(
                border: OutlineInputBorder(

                  borderSide: BorderSide(color: Colors.blue)
                ),
                labelText: 'EMO Sicil No',
                prefixIcon: const Icon(Icons.person, color: Colors.blue,),
                hintText: "EMO Sicil No Giriniz",
              ),
            ),
          ),

I will use controller here. But I want to use the value of Emo Sicil No under Uploader class inside DocUpload.dart

         class Uploader extends StatefulWidget {
         final File file;

         Uploader({Key key, this.file}) : super(key: key);

         createState() => _UploaderState();
           }

          class _UploaderState extends State<Uploader> {
          final FirebaseStorage _storage =
          FirebaseStorage(storageBucket: 'gs://emo-is0.appspot.com');

             StorageUploadTask _uploadTask;

             _startUpload() async {

              String filePath = 'foyler/${DateTime.now()}.png';

// I want to use Emo Sicil No here. The structure of the filePath will be ;

            String filePath = 'emosicilno/foyler/${DateTime.now()}.png';

This emo sicil no will come from texfield under homescreen.dart

              StorageUploadTask _uploadTask = 
                         _storage.ref().child(filePath).putFile(widget.file);

I am not sure how I can handle such. I need to get the value of text field inside homescreen.dart and use this inside docupload.dart class uploader

Please advice!

3 Answers

You need to create setters to the class first, then make an instance of the class under:

    class _UploaderState extends State<Uploader>{
        ClassName class = ClassName()

To get the value of the textfield you need to create a TextEditingController :

final myTextController = TextEditingController();

then associate it to the text field like that:

    TextField(
            **controller: myTextController,**
                  decoration: InputDecoration(
                    border: OutlineInputBorder(
    
                      borderSide: BorderSide(color: Colors.blue)
                    ),
                    labelText: 'EMO Sicil No',
                    prefixIcon: const Icon(Icons.person, color: Colors.blue,),
                    hintText: "EMO Sicil No Giriniz",
                  ),
                ),

To get the value of the textfield just write myTextController.text

I can get the text under the home screen. How I can pass it to another class uploader under docupload.dart?

As I understood you want to use Value of textFiled from Home screen to Uploader screen. For this, you can get the value of textFiled from the controller at the time of navigating to the next screen(myTextController.text) and pass it as an argument to the next screen.

If I sent my code to you can you please do it for me if possible since I can not do that?

For that kind of behavior, you might want to use some state management solution for Flutter. Such as Provider or Bloc.

With those, you can create a variable accessible in any widget, update its value and notify all places where it's being used that has been updated to rebuild them.

Related