0

When a user clicks on approve button.

When a user clicks on approve button, a lot of adding and deleting happens in the background. If the user clicks on bottomnavBar to change the screen, the background processing stops. When loading screen appears

on press code :

                      Consumer<TimeSheets>(
                                      builder: (ctx, timeSheetsData, _) =>
                                          TextButton(
                                              onPressed: () async {
                                                // callApi1();
                                                // readData();
                                                //workingg
                                                _isLoading = true;
                                                widget.callback(_isLoading);
                                                if (!mounted) return;
                                                setState(() {});
                                                await Provider.of<
                                                            pending_Sheets>(
                                                        context,
                                                        listen: false)
                                                    .add_pending_Sheets(
                                                        timeSheetsData.items);
                                                await Provider.of<TimeSheets>(
                                                        context,
                                                        listen: false)
                                                    .deleteTimeSheet(
                                                        timeSheetsData.items);
                                                _isLoading = false;
                                                widget.callback(_isLoading);
                                                if (!mounted) return;
                                                setState(() {});
                                                print(
                                                    'cac ${timeSheetsData.items}');
                                              },
                                              child: const Text(
                                                'Send For Approval',
                                                style: TextStyle(
                                                    color: kPrimaryColor),
                                              )),
                                    ),

whole widget code

class MyTimeSheets_page extends StatefulWidget {
  const MyTimeSheets_page({Key? key, required this.callback}) : super(key: key);
  final Function(bool) callback;
  @override
  State<MyTimeSheets_page> createState() => _MyTimeSheets_pageState();
}

class _MyTimeSheets_pageState extends State<MyTimeSheets_page> {
  late DateTime fromDate;
  late DateTime toDate;
  // late Future<dynamic> userDataFuture;
  var _isLoading = false;
  // var _childLoading = false;
  // var auth_provider;
  @override
  void initState() {
    super.initState();
    fromDate = DateTime.now();
    toDate = DateTime.now().add(Duration(hours: 2));
    print('after suppr');
  }

  var _isInit = true;

  //var _isLoading = false;
  @override

  Future _refreshment(BuildContext context) async {
    //${prefs.getString("uid")}
/*     auth_provider =
        await Provider.of<AuthenticationProvider>(context, listen: false); */
    return Provider.of<TimeSheets>(context, listen: false)
        .fetchAndSetTimeSheets(await customJugaar() ? true : false);
  }

  @override
  Widget build(BuildContext context) {
  
    print('rebuilding...');

    return _isLoading
        ? Loading_Custom(
            txtData: 'Sending For Approval ...',
          )
        : FutureBuilder(
            future: _refreshment(context),
            builder: (ctx, snapshot) => snapshot.connectionState ==
                    ConnectionState.waiting
                ? Loading_Custom(
                    txtData: '',
                  )
                : RefreshIndicator(
                    color: kPrimaryColor,
                    onRefresh: () => _refreshment(context),
                    child: ListView(
                      physics: const AlwaysScrollableScrollPhysics(),
                      //NeverScrollableScrollPhysics(),
                      children: [
                        Column(
                          mainAxisSize: MainAxisSize.min,
                          children: <Widget>[
                            Padding(
                              padding: const EdgeInsets.fromLTRB(13, 3, 13, 0),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    Utils.toDate(fromDate),
                                    style: TextStyle(
                                        fontWeight: FontWeight.bold,
                                        fontSize: 15,
                                        color: Colors.grey),
                                  ),
                                  /*   Text('1/7',
                                style: TextStyle(
                                    fontWeight: FontWeight.bold,
                                    fontSize: 15,
                                    color: Colors.grey)) */
                                  Align(
                                    alignment: Alignment.topRight,
                                    child: Consumer<TimeSheets>(
                                      builder: (ctx, timeSheetsData, _) =>
                                          TextButton(
                                              onPressed: () async {
                                                // callApi1();
                                                // readData();
                                                //workingg
                                                _isLoading = true;
                                                widget.callback(_isLoading);
                                                if (!mounted) return;
                                                setState(() {});
                                                await Provider.of<
                                                            pending_Sheets>(
                                                        context,
                                                        listen: false)
                                                    .add_pending_Sheets(
                                                        timeSheetsData.items);
                                                await Provider.of<TimeSheets>(
                                                        context,
                                                        listen: false)
                                                    .deleteTimeSheet(
                                                        timeSheetsData.items);
                                                _isLoading = false;
                                                widget.callback(_isLoading);
                                                if (!mounted) return;
                                                setState(() {});
                                                print(
                                                    'cac ${timeSheetsData.items}');
                                              },
                                              child: const Text(
                                                'Send For Approval',
                                                style: TextStyle(
                                                    color: kPrimaryColor),
                                              )),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            TimeSheetList(),
                            Padding(
                              padding: const EdgeInsets.fromLTRB(13, 13, 13, 2),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    'Weekly ${Utils.toWeek(fromDate)} - ${Utils.toWeek1(fromDate.add(Duration(days: 7)))}',
                                    style: TextStyle(
                                        fontWeight: FontWeight.bold,
                                        fontSize: 15,
                                        color: Colors.grey),
                                  ),
                                ],
                              ),
                            ),
                            Divider(
                              height: 20,
                              thickness: 2,
                            )
                          ],
                        ),
                      ],
                    ),
                  ),
          );
  }
}

And this error occurs.

This error can occur when code calls setState() from a timer or an animation callback.
The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. 
Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.
This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree.
**strong text** To avoid memory leaks, consider breaking the reference to this object during dispose().

I want user to be able to change the screens but the background processing should never be stopped. How can I achieve this?

0 Answers0