0

Because I want to call the setState Method in the class "EditProfilePage", I had to change it to a statefulWidget:

class EditProfilePage extends StatefulWidget {

  @override
  _EditProfilePageState createState() => _EditProfilePageState();

}

class _EditProfilePageState extends State<EditProfilePage> {

  TextEditingController nameController = new TextEditingController();
  TextEditingController bioController = new TextEditingController();
   File file;

       .
       .
       .

     applyChanges() {
    Firestore.instance
        .collection('insta_users')
        .document(currentUserModel.id)
        .updateData({
      "displayName": nameController.text,
      "bio": bioController.text

    });
  }
 }

But now, my class 'Profile_page' seems to miss the method "applyChanges()" of "EditProfilePage" and throws following error:

The method 'applyChanges' isn't defined for the class 'EditProfilePage'.

Here is the method in 'Profile_Page', which calls applyChanges() from 'EditProfilePage':

editProfile() {
    EditProfilePage editPage = new EditProfilePage();

    Navigator.of(context)
        .push(new MaterialPageRoute<bool>(builder: (BuildContext context) {
      return new Center(
        child: new Scaffold(
            appBar: new AppBar(
              leading: new IconButton(
                icon: new Icon(Icons.close),
                onPressed: () {
                  Navigator.maybePop(context);
                },
              ),
              title: new Text('Edit Profile',
                  style: new TextStyle(
                      color: Colors.black, fontWeight: FontWeight.bold)),
              elevation: 1.0,
              backgroundColor: Colors.white,
              actions: <Widget>[
                new IconButton(
                    icon: new Icon(
                      Icons.check,
                      color: Colors.blueAccent,
                    ),
                    onPressed: () {
                      editPage.applyChanges();
                      Navigator.maybePop(context);
                    })

Any suggestions on how to fix this problem? I did not forget to import.

Btw I am very new to flutter and just trying to understand dart. Best Regards!

Martin Seubert
  • 978
  • 3
  • 21
  • 37
  • Possible duplicate of [call method in one stateful widget from another stateful widget - Flutter](https://stackoverflow.com/questions/51029655/call-method-in-one-stateful-widget-from-another-stateful-widget-flutter) – mirkancal Mar 25 '19 at 11:56

1 Answers1

0

You should make significant change to your EditProfilePage structure by moving the applyChanges function away from _EditProfilePageState class:

   class EditProfilePage extends StatefulWidget {

   @override
   _EditProfilePageState createState() => _EditProfilePageState();

     static void applyChanges(TextEditingController nameController, TextEditingController bioController) {
      Firestore.instance.collection('insta_users').document(currentUserModel.id).updateData({
      "displayName": nameController.text,
      "bio": bioController.text
      });
     }

   }

Then call applyChanges() function from inside the function editProfile() :

  EditProfilePage.applyChanges(nameController, bioController);

But you have to make your TextEditingController objects which contain the name and bio visible to the context you are calling from. One way I would suggest is to make them global by defining them under the import section of your code which will enable any inner class to use them.

Mazin Ibrahim
  • 7,433
  • 2
  • 33
  • 40