简体   繁体   中英

How to get context from static method flutter

Is there a way to get context from a static method in flutter? Say I have a function that requires a context eg Navigator.pop(context) , the context property won't get recognised. So, is it possible to pass current class context to that static method so I can use its context ?

//static method

Future<dynamic> _myBackgroundMessageHandler
    (Map<String, dynamic> message) async {
  print("onBackground Message called");
  print(message);
  PushNotificationService().fetchRideInfo(message["data"]["orderId"],
      PushNotificationService._context,
      "onBackground");
//need to access context from here but not working
  print("onBackground");
  return PushNotificationService().showNotification(message);
}


class PushNotificationService{
  static BuildContext _context;

  static init({@required BuildContext context}) {
    _context = context;
  }

    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
        fetchRideInfo(message['data']['orderId'], context, "onMessage");
      },
    onBackgroundMessage: Platform.isIOS ? null:_myBackgroundMessageHandler,

      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
        fetchRideInfo(message['data']['orderId'], context, "onResume");
      },
    );
    ///fetch user messaging token here
    getToken();
  }
  void fetchRideInfoInBackground() {
    print("incoming Request");
    assetsAudioPlayer.open(
      Audio('sounds/alert.mp3'),
    );
    assetsAudioPlayer.play();
  }
  Future<String> getToken() async{
    print("fetching token");
    token = await _firebaseMessaging.getToken();
    print('token got as: $token');
    return token;

  }

  void fetchRideInfo(orderId, context, String type) {
    print("fetching info");
    showDialog(
      barrierDismissible: false,
      context: context,
      builder: (BuildContext context) =>
          CustomProgressDialog(status:'Fetching details',),);
    if(type!="onResume"){
      assetsAudioPlayer.open(
        Audio('sounds/alert.mp3'),
      );
      assetsAudioPlayer.play();
      print(orderId);
    }

    orderIdString = orderId;

      Provider.of<MainBloc>(context, listen: false).
      fetchRideInfo(context, orderId).then((value){
        Navigator.pop(context);
        Navigator.push(
          context,
          MaterialPageRoute(builder: (context) =>
              RideRequestPage(orderId: orderId)),
        );
      }).catchError((error) {
        Navigator.pop(context);
        AlertManager.showToast(error.toString());
      });

  }

}

If you want to have the context, you can just use the Builder class:

Builder(
builder: (BuildContext context) => "Here you can put everything and use the Context";
 ),

To learn more about Builder: https://api.flutter.dev/flutter/widgets/Builder-class.html

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