简体   繁体   English

如何从另一个有状态小部件调用一个有状态小部件中的方法

[英]how to Call method in one stateful widget from another stateful widget

I have a method called Counter() In the homepage StatefullWidget, i want to call this method from my main.dart.我在主页 StatefullWidget 中有一个名为Counter()的方法,我想从我的 main.dart 调用这个方法。

I am trying to call the method Counter() from my main.dart file.我正在尝试从我的 main.dart 文件中调用方法 Counter() 。 i post the code below我在下面发布代码

 here is my homepage, class

    import 'package:flutter/material.dart';
    class homepage extends StatefulWidget {
      const homepage({Key? key}) : super(key: key);
    
      @override
      _homepageState createState() => _homepageState();
    }
    
    class _homepageState extends State<homepage> {
      @override
      Widget build(BuildContext context) {
        return Container();
      }
      Counter(){
        //I want to call this method from my main.dart file
      }
    }

   Here is my main.dart file
import 'package:flutter/material.dart';
        Future<void> backgroundhandller(RemoteMessage message) async {
          **Counter()** //I want to call here
            
        }
        
        void main() async {
          FirebaseMessaging.onBackgroundMessage(backgroundhandller);
          runApp(MaterialApp(
            debugShowCheckedModeBanner: false,
            title: "Taxiyee_Messaging_app",
            home: LoginScreen(),
          ));
        }

You should declare your Counter() method as a static (outside of your build() method).您应该将您的Counter()方法声明为static (在您的build()方法之外)。

static void Counter() {
   //Your code
}

and call it by using (remember to import the homepage into the main.dart file)并使用调用(记得将主页导入main.dart文件)

Future<void> backgroundhandller(RemoteMessage message) async {
      homepage.Counter();
}

This may be helpful . 这可能会有所帮助

The _homepageState class is private because of the preceding _ you can't access it. _homepageState class 是私有的,因为前面的_你不能访问它。 however you can move the method to the homepage class and then you can call it from anywhere you imported it.但是你可以把这个方法移到homepage class 然后你可以从你导入它的任何地方调用它。

also you can still use the counter method inside your _homepageState class by using the widget object like this widget.counter()您还可以通过使用小部件 object 像这样widget.counter()_homepageState class 中使用计数器方法

This would be the end code:这将是结束代码:

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

  void count(){
    //I want to call this method from my main.dart file
  }

  @override
  _HomepageState createState() => _HomepageState();
}

class _HomepageState extends State<Homepage> {
  @override
  Widget build(BuildContext context) {
    
    widget.count(); // This how you use it inside your State

    return Container();
  }
  
}

Here is how to use it inside the main.dart file以下是如何在 main.dart 文件中使用它

import 'package:flutter/material.dart';
import 'homepage.dart';

Future<void> backgroundhandller(RemoteMessage message) async {
  Homepage().count() //call it here or anywhere like this      
}
    
void main() async {
  FirebaseMessaging.onBackgroundMessage(backgroundhandller);
  runApp(MaterialApp(
    debugShowCheckedModeBanner: false,
    title: "Taxiyee_Messaging_app",
    home: LoginScreen(),
  ));
}

you notice I've done some changes to the names to follow convention.您会注意到我已经对名称进行了一些更改以遵循惯例。 Classes names start with Capital letters methods/function with small letters.类名以大写字母开头,方法/函数以小写字母开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从另一个有状态小部件调用一个有状态小部件中的方法 - Flutter - call method in one stateful widget from another stateful widget - Flutter 如何在另一个有状态小部件中调用方法 - How to call a method in another stateful widget 将字符串值从一个有状态小部件更改为另一个有状态小部件 - Change String value from one stateful widget to another stateful widget 如何使用来自另一个 Widget 的 Stateful Widget 中的方法 - How to use a method in a Stateful Widget from another Widget 如何从有状态小部件内的有状态小部件调用方法 - How do I call a method from a stateful widget inside of a stateful widget 如何在 Flutter 中从另一个有状态小部件更改一个有状态小部件的状态? - How to change state of one Stateful Widget from another Stateful Widget in Flutter? 如何将基于键的对象从一个有状态小部件传递到另一个有状态小部件 - How to pass objects based on key from one stateful widget to another stateful widget 如何在另一个有状态小部件中调用 Void() function - How to call Void() function in another Stateful widget 如何从另一个有状态小部件调用有状态小部件中的函数? - How to call a function in a Stateful Widget from another StatefulWidget? Flutter:如何从另一个有状态类更新 UI 状态小部件 - Flutter:How to update a UI -stateful widget from another stateful class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM