Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to return a value from an async function which also awaits for a value

I am new to Flutter and the Dart programming language. I am making a shift-assignment app, but am stuck on this – I need to return a string from an async function which also waits for a value from other async functions. This is what I have written so far:

Future<String> getNumberOfShiftsText() async {
    int weekShiftCount = 0;
    List<DateTime> shiftTimes = [];
    await getUserDetailsFromServer().then((value){
      await getAppointmentsFromServer(cid, isEmployer, rolesList).then((aValue) {
        List<HiveAppointment> appointmentList = appointmentBox.values.toList();
        for (int i = 0; i < appointmentList.length; i++) {
          if (appointmentList[i].startTime.isBefore(DateTime.now().add(const Duration(days: 7)))) {
            weekShiftCount += 1;
            shiftTimes.add(appointmentList[i].startTime);
          }
        }
      });
    });
    return Future.delayed(const Duration(seconds: 1), () => '$weekShiftCount shift(s) assigned to you in the next 7 days',);
  }

On running this, I get the error:

A non-null value must be returned since the return type ‘String’ doesn’t allow null.
Future getNumberOfShiftsText() async {

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any ideas on how I can solve this? Any help would be much appreciated. Thanks in advance, and I apologize for my lack of knowledge.

>Solution :

I am not really sure about the core issue of your problem. But your code does contain a problematic pattern with mixing then() and await which can make your program run in an unexpected way.

I suggest rewrite your code into something like this:

Future<String> getNumberOfShiftsText() async {
  int weekShiftCount = 0;
  List<DateTime> shiftTimes = [];
  final value = await getUserDetailsFromServer();
  final aValue = await getAppointmentsFromServer(cid, isEmployer, rolesList);

  List<HiveAppointment> appointmentList = appointmentBox.values.toList();
  for (int i = 0; i < appointmentList.length; i++) {
    if (appointmentList[i]
        .startTime
        .isBefore(DateTime.now().add(const Duration(days: 7)))) {
      weekShiftCount += 1;
      shiftTimes.add(appointmentList[i].startTime);
    }
  }

  return '$weekShiftCount shift(s) assigned to you in the next 7 days';
}

I am not sure about your value variable since you are not using it in your code. Also the "wait 1 second before returning" has been removed since I think you added this because you did not know when the async code was done.

The rewritten code fixes this so when the return statement are executed, we know the other code are done being executed.

If you still have issues, can you provide a stacktrace combined with the full error so I know where the issue might be.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading