title: How to get firebase device token
body: <p>Can someone help me with this. I want to change &quot;tokenas12345&quot; on line 65 in main.dart to Firebase getToken. How can I do that? I have file notification.dart, where on line 47 we can see how we get device token. Thank you.
  My main.dart file looks like this:</p>
  <pre><code>import 'dart:async';
  
  import 'package:flutter/material.dart';
  import 'package:webview_flutter/webview_flutter.dart';
  import 'package:firebase_core/firebase_core.dart';
  
  import 'notification.dart';
  
  void main() async {
    await init();
    runApp(const MyApp());
  }
  
  Future init() async {
    WidgetsFlutterBinding.ensureInitialized();
    await Firebase.initializeApp();
  }
  
  class MyApp extends StatelessWidget {
    const MyApp({Key? key}) : super(key: key);
  
    @override
    Widget build(BuildContext context) {
      return const MaterialApp(
        home: HomePage(),
      );
    }
  }
  
  class HomePage extends StatefulWidget {
    const HomePage({Key? key}) : super(key: key);
  
    @override
    State&lt;HomePage&gt; createState() =&gt; _HomePageState();
  }
  
  class _HomePageState extends State&lt;HomePage&gt; {
    String notificationTitle = 'No Title';
    String notificationBody = 'No Body';
    String notificationData = 'No Data';
  
    @override
    void initState() {
      final firebaseMessaging = FCM();
      firebaseMessaging.setNotifications();
  
      firebaseMessaging.streamCtlr.stream.listen(_changeData);
      firebaseMessaging.bodyCtlr.stream.listen(_changeBody);
      firebaseMessaging.titleCtlr.stream.listen(_changeTitle);
  
      super.initState();
    }
  
    _changeData(String msg) =&gt; setState(() =&gt; notificationData = msg);
    _changeBody(String msg) =&gt; setState(() =&gt; notificationBody = msg);
    _changeTitle(String msg) =&gt; setState(() =&gt; notificationTitle = msg);
  
    final Completer&lt;WebViewController&gt; _controller = Completer&lt;WebViewController&gt;();
    @override
    Widget build(BuildContext context){
      return new Scaffold(
          body: WebView(
            javascriptMode: JavascriptMode.unrestricted,
            onWebViewCreated: (WebViewController webViewController) {
              Map&lt;String, String&gt; headers = {&quot;x-device-id&quot;: &quot;tokenas12345&quot;};
              webViewController.loadUrl('https://www.google.com/', headers: headers);
  
  
            },
          )
      );
    }
  }
  </code></pre>
  <p>And my notification.dart file looks like this:</p>
  <pre><code>import 'dart:async';
  
  import 'package:firebase_core/firebase_core.dart';
  import 'package:firebase_messaging/firebase_messaging.dart';
  
  Future&lt;void&gt; onBackgroundMessage(RemoteMessage message) async {
    await Firebase.initializeApp();
  
    if (message.data.containsKey('data')) {
      // Handle data message
      final data = message.data['data'];
    }
  
    if (message.data.containsKey('notification')) {
      // Handle notification message
      final notification = message.data['notification'];
    }
    // Or do other work.
  }
  
  class FCM {
    final _firebaseMessaging = FirebaseMessaging.instance;
  
    final streamCtlr = StreamController&lt;String&gt;.broadcast();
    final titleCtlr = StreamController&lt;String&gt;.broadcast();
    final bodyCtlr = StreamController&lt;String&gt;.broadcast();
  
    setNotifications() {
      FirebaseMessaging.onBackgroundMessage(onBackgroundMessage);
      FirebaseMessaging.onMessage.listen(
            (message) async {
          if (message.data.containsKey('data')) {
            // Handle data message
            streamCtlr.sink.add(message.data['data']);
          }
          if (message.data.containsKey('notification')) {
            // Handle notification message
            streamCtlr.sink.add(message.data['notification']);
          }
          // Or do other work.
          titleCtlr.sink.add(message.notification!.title!);
          bodyCtlr.sink.add(message.notification!.body!);
        },
      );
      // With this token you can test it easily on your phone
      final token =
      _firebaseMessaging.getToken().then((value) =&gt; print('Token: $value'));
    }
  
    dispose() {
      streamCtlr.close();
      bodyCtlr.close();
      titleCtlr.close();
    }
  }
  </code></pre>