Urgent Flutter Code Crash Fix

Job ID: 37875194

Budget: $10 – $30 USD

project target: to provide a way to correctly call setstate inside stateful widget from within workmanager callbackdipatcher method

the following is a sample code demonstrates the problem, in this code the main gui displays a counter ,
workmanager callbackdipatcher method which is called every 15 minutes to increase the counter then callback setstate to update the UI and display the updated counter value
the code is causing exception and never works

we need to fix this code and make it works provided that all code semantics' remains the same, in other words the following points should be kept in the working code

1- the use of workmanager
2- the use of callbackDispatcher every 15 minutes
3- the use of calling setstate to update the UI

in addition to that the working code should also provide the following

1- work on android 7 upwords
2- work on IOS 15 upwords


general terms:
⦁ people whom are wants to participate on this project are required to proof the experience on flutter programming on android and ios
⦁ code is required to be clean and well documented, comment are required to illustrate every section and function of the code
⦁ variable names used in the code are required to be illustrative and describes its functionality
⦁ error handling should be in mind when designing the code
⦁ candidate is free to ask any questions so that requirements are clear in his mind
⦁ once agreed, selected cadidate needs to provide video for his implementation shows the system working on a real android and real iphone devices
⦁ if the video is ok and show the system working fine, candidate to send the the full flutter project to check it
⦁ if the code is working fine in my environment, money will be relaeased
⦁ please don't waste my time by using code already on the net, also don't use any AI platform in this project
⦁ budget 25$


============================================= sample code that needs to be fixed===================================================

import 'package:flutter/material.dart';
import 'package:workmanager/workmanager.dart';

void main() {
runApp(MyApp());
Workmanager().initialize(callbackDispatcher);
Workmanager().registerPeriodicTask(
"1",
"counterTask",
frequency: Duration(minutes: 15),
initialDelay: Duration(seconds: 30),
);
}

void callbackDispatcher() {
Workmanager().executeTask((taskName, inputData) {
// Access the instance of MyHomePage and call incrementCounter
_MyHomePageState()._incrementCounter();
return Future.value(true);
});
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Counter',
home: MyHomePage(),
);
}
}

class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;

void _incrementCounter() {
setState(() {
_counter++;
print("Counter: $_counter");
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter Counter'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Counter:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
);
}
}

============================================= sample code that needs to be fixed===================================================
Related categories: Mobile App Development iPhone Android Flutter