【问题标题】:A value of type 'Null' can't be returned from the method 'fetchById' because it has a return type of 'Location'无法从方法“fetchById”返回“Null”类型的值,因为它的返回类型为“Location”
【发布时间】:2021-09-14 21:01:34
【问题描述】:
static Location fetchById(int id) {
   List<Location> locations = Location.fetchAll();

   for (var i = 0; i < locations.length; i++) {
     if (locations[i].id == id) {
       return locations[i];
     }
   }
   
   return null;
}

// 如果条件不为真,则当我尝试返回 null 或 false 时返回 null “位置”的类型。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    使用 dart 语言中的null-safety 功能,您必须明确告知是否要使值可以为空。

    ?定义返回类型,这样dart就知道返回值可以为null。

    static Location? fetchById(int id) 
    { 
      /// function body
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 2021-01-25
      • 2021-09-27
      • 2022-01-26
      • 1970-01-01
      相关资源
      最近更新 更多