简体   繁体   中英

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.

I am trying to call the method Counter() from my main.dart file. 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).

static void Counter() {
   //Your code
}

and call it by using (remember to import the homepage into the main.dart file)

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. however you can move the method to the homepage class and then you can call it from anywhere you imported it.

also you can still use the counter method inside your _homepageState class by using the widget object like this widget.counter()

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM