[英]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();
}
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.