Flutter Isolate doesn't go do setState after navigate to a other page and back


Hello I have a badge with a counter and whenever I start my Android Alarm Manager it should increase the counter and also update the UI live. I do this with Isolate. Everything works fine but when I go to a new page route and then go back the SetState is not executed anymore. Where is my error?

  int _counter = 0;

  String isolateName = 'isolate';
  final ReceivePort port = ReceivePort();
  static SendPort uiSendPort;

  void initState() {


    port.listen((_) async => await _incrementCounter());

  Future<void> _incrementCounter() async {
    print('Increment counter!');

    // Ensure we've loaded the updated count from the background isolate.
    await prefs.reload();

    if (!mounted) return;
    setState(() {
      print("geht in die SetState");

///This is in my Callback for my Android Alarm Manager...
          final prefs = await SharedPreferences.getInstance();
          int currentCount = prefs.getInt(countKey) ?? 0;
          await prefs.setInt(countKey, currentCount + 1);

          // This will be null if we're running in the background.
          uiSendPort ??= IsolateNameServer.lookupPortByName('isolate');


initState is not called again after returning, try to force the function you want after returning

something like that

     builder: (context) => YourScreenâ„–2(),
  .then((value) {
    Foo() <- your func

Answered By – novol

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *