title: unable to send messages From SendPort to ReceivePort in flutter
body: <p>I'm trying to send a message via the IsolateNameServer in flutter.</p>
  
  <p>However, I am unable to receive messages. I've tried checking if the receiver port is listening by calling <code>port.listen()</code> twice, and it says that it is already listening.</p>
  
  <p>Where am I going wrong? [ I am closely following <a href="https://pub.dev/packages/android_alarm_manager#-example-tab-" rel="nofollow noreferrer">this documentation</a> ]</p>
  
  <p>Here is my main.dart, based on the above doc:</p>
  
  <pre><code>import 'dart:isolate';
  import 'dart:math';
  import 'dart:ui';
  import 'package:android_alarm_manager/android_alarm_manager.dart';
  import 'package:flutter/material.dart';
  
  final ReceivePort port = ReceivePort();
  const String isolateName = 'isolate';
  
  main() async {
    WidgetsFlutterBinding.ensureInitialized();
  
    IsolateNameServer.registerPortWithName(
      port.sendPort,
      isolateName,
    );
  
    runApp(MyApp());
  }
  
  class MyApp extends StatelessWidget {
    // This widget is the root of your application.
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
              primarySwatch: Colors.blue,
              accentColor: Colors.pink,
              floatingActionButtonTheme:
                  FloatingActionButtonThemeData(backgroundColor: Colors.pink)),
          home: AlarmManagerExample());
    }
  }
  
  class AlarmManagerExample extends StatefulWidget {
    @override
    _AlarmManagerExampleState createState() =&gt; _AlarmManagerExampleState();
  }
  
  class _AlarmManagerExampleState extends State&lt;AlarmManagerExample&gt; {
    @override
    void initState() {
      super.initState();
      AndroidAlarmManager.initialize();
      port.listen((_) async =&gt; await workForMe());
    }
  
    workForMe() async {
      print("Secondary Function Triggered!");
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(),
        body: Container(
          child: RaisedButton(
            onPressed: () async {
              await AndroidAlarmManager.oneShot(const Duration(seconds: 5),
                  Random().nextInt(pow(2, 21)), callback);
            },
            child: Text(
              "Alarm Manager",
            ),
          ),
        ),
      );
    }
  
    static SendPort uiSendPort;
  
    static callback() {
      print("Callbacks Triggered!");
  
      // This will be null if we're running in the background.
      uiSendPort ??= IsolateNameServer.lookupPortByName(isolateName);
      uiSendPort?.send(null);
    }
  }
  
  </code></pre>
  
  <p>The output I am getting is : </p>
  
  <pre><code>Callbacks Triggered!
  </code></pre>
  
  <p>However, the expected output is: </p>
  
  <pre><code>Callbacks Triggered!
  Secondary Function Triggered!
  </code></pre>