【问题标题】:Method is not defined in other class方法未在其他类中定义
【发布时间】:2019-03-25 11:21:43
【问题描述】:

因为要调用类"EditProfilePage"中的setState方法,所以只好改成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

    });
  }
 }

但是现在,我的班级 'Profile_page' 似乎错过了“EditProfilePage”的方法“applyChanges()”并抛出以下错误:

没有为类定义方法“applyChanges” 'EditProfilePage'。

这是'Profile_Page'中的方法,它从'EditProfilePage'调用applyChanges()

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);
                    })

关于如何解决此问题的任何建议?我没有忘记导入。

顺便说一句,我对扑腾很陌生,只是想了解飞镖。最好的问候!

【问题讨论】:

标签: flutter statefulwidget


【解决方案1】:

您应该通过将applyChanges 函数从_EditProfilePageState 类移开来对EditProfilePage 结构进行重大更改:

   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
      });
     }

   }

然后从函数editProfile()内部调用applyChanges()函数:

  EditProfilePage.applyChanges(nameController, bioController);

但是您必须使包含namebioTextEditingController 对象对您调用的上下文可见。我建议的一种方法是通过在代码的导入部分下定义它们来使它们全局化,这将使任何内部类都可以使用它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 1970-01-01
    • 2019-03-05
    相关资源
    最近更新 更多