I have 3 issues from migrating to null-safety

Issue

I migrated to null-safety today and I have 3 issues that I don’t know how to fix. Below, I have provided the errors and the code that each error refers to. I do not know how to fix these issues and they are the only errors left.

error • The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type at lib\Services\firestore_service.dart:29:16 • (body_might_complete_normally)

Future<Void> saveNewAgency(Agency agency) async {
    await _db.collection('agency').doc(globals.agencyId).set(agency.toMap());
  }

      error • The property 'docs' can't be unconditionally accessed because the receiver can be 'null' at lib\screens\agent_dash_board.dart:57:43 •(unchecked_use_of_nullable_value)
I fixed similar errors to this one with the code at itemCount: (snapshot.data! as QuerySnapshot).docs.length,  in the code snippet. 
    
    return new ListView.builder(
                          itemCount: (snapshot.data! as QuerySnapshot).docs.length,
                          itemBuilder: (BuildContext context, int index) {
                            Trxns trxns = Trxns.fromFirestore(
                                snapshot.data.docs[index].data());

  error • The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type at lib\screens\appointment_calendar.dart:434:31 • (body_might_complete_normally)

Map<DateTime, List<Event?>> convertToMap(List<Event> item) {
    Map<DateTime, List>? result;

    for (int i = 0; i < item.length; i++) {
      Event data = item[i];
      //get the date and convert it to a DateTime variable
      //DateTime currentDate = DateFormat('MM-dd-yyyy - kk:mm').format(data.eventDate);
      DateTime currentDate = DateTime(data.eventDate!.year, data.eventDate!.month, data.eventDate!.day, data.eventDate!.hour, data.eventDate!.minute);

      List eventNames = [];
      //add the event name to the the eventNames list for the current date.
      //search for another event with the same date and populate the eventNames List.
      for (int j = 0; j < item.length; j++) {
        //create temp calendarItemData object.
        Event temp = item[j];
        //establish that the temp date is equal to the current date
        if (data.eventDate == temp.eventDate) {
          //add the event name to the event List.
          eventNames.add(temp.eventName);
        } //else continue
      }

      //add the date and the event to the map if the date is not contained in the map
      if (result == null) {
        result = {
          currentDate: eventNames
        };
      } else {
        result[currentDate] = eventNames;
      }
      return result as Map<DateTime, List<Event?>>;
    }
  }

Solution

Future<Void> saveNewAgency(Agency agency) async {
  await _db.collection('agency').doc(globals.agencyId).set(agency.toMap());
}

Try returning Future<void> I’m not sure where you are getting Void with a capital V from.


    return new ListView.builder(
                          itemCount: (snapshot.data! as QuerySnapshot).docs.length,
                          itemBuilder: (BuildContext context, int index) {
                            Trxns trxns = Trxns.fromFirestore(
                                snapshot.data.docs[index].data());

The solution as discussed below was to do this:

Trxns trxns = Trxns.fromFirestore((snapshot.data! as QuerySnapshot).docs[index]!.data() as Map<String, dynamic>);

Map<DateTime, List<Event?>> convertToMap(List<Event> item) {
    Map<DateTime, List>? result;

    for (int i = 0; i < item.length; i++) {
      Event data = item[i];
      //get the date and convert it to a DateTime variable
      //DateTime currentDate = DateFormat('MM-dd-yyyy - kk:mm').format(data.eventDate);
      DateTime currentDate = DateTime(data.eventDate!.year, data.eventDate!.month, data.eventDate!.day, data.eventDate!.hour, data.eventDate!.minute);

      List eventNames = [];
      //add the event name to the the eventNames list for the current date.
      //search for another event with the same date and populate the eventNames List.
      for (int j = 0; j < item.length; j++) {
        //create temp calendarItemData object.
        Event temp = item[j];
        //establish that the temp date is equal to the current date
        if (data.eventDate == temp.eventDate) {
          //add the event name to the event List.
          eventNames.add(temp.eventName);
        } //else continue
      }

      //add the date and the event to the map if the date is not contained in the map
      if (result == null) {
        result = {
          currentDate: eventNames
        };
      } else {
        result[currentDate] = eventNames;
      }
      return result as Map<DateTime, List<Event?>>;
    }
  }

I think you meant to put that return statement after the for loop, not inside of it.

Answered By – mmcdon20

Answer Checked By – Jay B. (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.