【问题标题】:I have 3 issues from migrating to null-safety我有 3 个从迁移到 null-safety 的问题
【发布时间】:2021-06-25 17:14:30
【问题描述】:

我今天迁移到 null-safety,但有 3 个问题我不知道如何解决。下面,我提供了错误和每个错误所指的代码。我不知道如何解决这些问题,它们是唯一剩下的错误。

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?>>;
    }
  }

【问题讨论】:

    标签: flutter dart dart-null-safety


    【解决方案1】:
    Future<Void> saveNewAgency(Agency agency) async {
      await _db.collection('agency').doc(globals.agencyId).set(agency.toMap());
    }
    

    尝试返回 Future&lt;void&gt; 我不确定您从哪里获得带有大写 V 的 Void


        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());
    

    下面讨论的解决方案是这样做:

    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?>>;
        }
      }
    

    我认为你的意思是把 return 语句放在 for 循环之后,而不是放在里面。

    【讨论】:

    • 感谢您的帮助。第一个和第三个修复工作,但第二个没有工作。我进行了您建议的更改并收到此错误:未在 lib\screens\agent_dash_board.dart:57:44 为类型“对象”定义吸气剂“文档”。这是现在的代码:Trxns? trxns = Trxns.fromFirestore(snapshot.data?.docs[index].data());
    • 您能否在围绕第二个问题的代码中添加更多上下文?也许您可以尝试Trxns trxns = Trxns.fromFirestore( (snapshot.data! as QuerySnapshot).docs[index].data());,因为它与您对itemCount 参数采取的方法一致。
    • 我尝试了您的建议并收到此错误:参数类型“对象?”不能分配给参数类型 'Map' 。这是 Trxn.fromFirestore() 代码 sn-p, Trxns.fromFirestore(Map firestore) : trxnId = firestore['trxnId'], agentId = firestore['agentId'];
    • .data() 意味着返回 Map&lt;String, dynamic&gt; 吗?看来fromFirestore 方法希望您传递Map&lt;String, dynamic&gt;。也许您可以将其转换为一个?喜欢:Trxns trxns = Trxns.fromFirestore( (snapshot.data! as QuerySnapshot).docs[index]!.data() as Map&lt;String, dynamic&gt;);
    • 修复了它。谢谢!
    猜你喜欢
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    相关资源
    最近更新 更多