【问题标题】:A value of type 'Null' can't be returned from the constructor无法从构造函数返回类型为“Null”的值
【发布时间】:2022-01-04 12:51:09
【问题描述】:

我正在用颤振/飞镖构建一个应用程序和 我正在尝试检查返回的路由信息​​是否为空,然后再继续进行其余的路由构建,我在“工厂”内执行此操作,如果 isEmpty 我将返回 null ,我收到以下错误 “无法从构造函数 'Directions.fromMap' 返回类型为 'Null' 的值,因为它的返回类型为 'Directions'” 感谢您的支持

class Directions{
  final LatLngBounds bounds; 
  final List<PointLatLng> plylinePoints;
  final String totalDistance;
  final String totalDuration;

  const Directions ({
    required this.bounds, required this.plylinePoints,
    required this.totalDistance, required this.totalDuration
  });

  factory Directions.fromMap(Map<String, dynamic> map) {
        if ((map['routes'] as List).isEmpty) return null;

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    Dart 中的factory 构造函数只允许您返回子类 的实例时间>。你不能从它返回null

    您可以做的是使用static Directions? fromMap(Map&lt;String, dynamic&gt; map) 函数

    这样你就可以返回 Directionnull

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-17
      • 2016-08-03
      • 2021-09-14
      • 1970-01-01
      • 2022-01-26
      • 2023-01-15
      相关资源
      最近更新 更多