flutterfirebasefirebase-cloud-messagingdevicetoken

How to get the device token in flutter


I'm trying to get the device token, for this i'm using FirebaseMessaging. here is my code to get the token.

@override
  void initState()  {
 FirebaseMessaging? _firebaseMessaging;
 _firebaseMessaging?.getToken().then((token){
      print("token is $token");
});
}


but it print nothing. Please help how to do this.


Solution

  • You have to create the FirebaseMessaging instance. Change to:

    @override
    void initState() {
      FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance; // Change here
      _firebaseMessaging.getToken().then((token){
        print("token is $token");
      });
    }