-1

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?>>;
    }
  }
LostTexan
  • 431
  • 5
  • 18

1 Answers1

1
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.

mmcdon20
  • 5,767
  • 1
  • 18
  • 26
  • thank you for your help. The first and third fixes worked but the second on did not work. I made the changes you suggested and get this error: The getter 'docs' isn't defined for the type 'Object' at lib\screens\agent_dash_board.dart:57:44 . Here is the code now: Trxns? trxns = Trxns.fromFirestore( snapshot.data?.docs[index].data()); – LostTexan Jun 25 '21 at 17:53
  • Can you add more context to the code surrounding the second problem? Perhaps you could try `Trxns trxns = Trxns.fromFirestore( (snapshot.data! as QuerySnapshot).docs[index].data());` since it is consistent with with the approach you took for the `itemCount` argument. – mmcdon20 Jun 25 '21 at 18:02
  • I tried your suggestion and got this error: The argument type 'Object?' can't be assigned to the parameter type 'Map' . Here is the Trxn.fromFirestore() code snippet, Trxns.fromFirestore(Map firestore) : trxnId = firestore['trxnId'], agentId = firestore['agentId']; – LostTexan Jun 25 '21 at 18:16
  • is `.data()` meant to return a `Map` ? It appears the `fromFirestore` method expects you to pass a `Map`. Maybe you can cast it to one? like: `Trxns trxns = Trxns.fromFirestore( (snapshot.data! as QuerySnapshot).docs[index]!.data() as Map);` – mmcdon20 Jun 25 '21 at 18:21