首页 > 解决方案 > Converting Firebase Token to String using Flutter

问题描述

I have an application that is supposed to send the Firebase Token from my Flutter app to an ASP.Net App Server. The endpoint on the app server works - the request from the Flutter app to the App Server is not working.

The reason it is not working is because when I try to send the token, the token doesn't appear to have arrived yet - it's of type Future. How do I turn that token into a string when it finally arrives?

I've tried turning the token directly into a string in the fcmStream.Listen function, I've also tried turning it into a string using _firebaseMessaging.getToken. Neither of them work

  FirebaseMessaging _firebaseMessaging = new FirebaseMessaging();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    location.onLocationChanged().listen((value) {
      if (this.mounted) {
        setState(() {
          currentLocation = value;
        });
      }
    });
_firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) {
        print('on message $message');
      },
      onResume: (Map<String, dynamic> message) {
        print('on resume $message');
      },
      onLaunch: (Map<String, dynamic> message) {
        print('on launch $message');
      },
    );
    _firebaseMessaging.requestNotificationPermissions(
        const IosNotificationSettings(sound: true, badge: true, alert: true));
    String clientToken = _firebaseMessaging.getToken().then((token) {
      print("Token Init: " + token.toString());
    }
    ).toString();


    BackendService.postToken(clientToken.toString(), "email@gmail.com");

    @override
    Stream<String> fcmStream = _firebaseMessaging.onTokenRefresh;
    fcmStream.listen((token) {
      /*print("Token On Refresh: " + token);
      BackendService.postToken(token.toString(), "email@gmail.com");*/

    }
    );
    fcmStream.toString();

class BackendService {
  static Future<String> postToken(String token, String hostEmail) async {
    final responseBody = (await http.get(
            'realurlomitted/.../Meets/RegisterDevice?token=$token&hostEmail=$hostEmail')).body;
    print(" Response: " + responseBody.toString());

    return responseBody.toString();
  }
}

Whenever the token.toString prints, it prints the token just fine - I can see that. It just seems like whenever it tries to make the post using http, the token hasn't arrived from whatever getToken is.

If I can turn that Futrure into a string by awaiting it or something, it would solve my problem so that the $token parameter is the token as a string.

More specifically, my request URL should look like:

https://-----/Meets/RegisterDevice?token=c6V49umapn0:Jdsf90832094890s324&hostEmail=email@gmail.com

But it looks like:

https://-----/Meets/RegisterDevice?token=instance of Future<Dynamic>&hostEmail=email@gmail.com

In the Flutter debugger

标签: firebaseflutterfirebase-cloud-messaging

解决方案


正如你所说,等待未来会解决你的问题。您可以编写一个async函数并将代码initState放入其中并使用await,或者您可以这样做:

_firebaseMessaging.getToken().then((token) {
      final tokenStr = token.toString();
      // do whatever you want with the token here
    }
);

推荐阅读