首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >参数类型为“List<Slots?>?”不能分配给参数类型'List<Slots>?‘

参数类型为“List<Slots?>?”不能分配给参数类型'List<Slots>?‘
EN

Stack Overflow用户
提问于 2022-05-04 13:25:23
回答 2查看 146关注 0票数 0

我最近将我的代码迁移到空安全。在dart迁移命令中得到以下错误。

参数类型为“List?”不能分配给参数类型'List?‘在lib/responsemodels/appointment/calendar/calendar_response.g.dart:17:5

突出显示下面的错误抛出行

calendar_response.dart

代码语言:javascript
复制
// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'calendar_response.dart';

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

CalenderResponse _$CalenderResponseFromJson(Map<String, dynamic> json) {
  return CalenderResponse(
    json['createdAt'] as String?,
    json['dayInWeek'] as String?,
    json['isHavingSlot'] as String?,
    json['message'] as String?,
    json['schedulerId'] as String?,
    json['selectedDate'] as String?,
    **(json['slotTimings'] as List?)
        ?.map(
            (e) => e == null ? null : Slots.fromJson(e as Map<String, dynamic>))
        ?.toList(),**
    json['status'] as String?,
    json['updatedAt'] as String?,
    json['currenttime'] as String?,
  );
}

CalenderAvailableDatesResponse _$CalenderAvailableDatesResponseFromJson(
    Map<String, dynamic> json) {
  return CalenderAvailableDatesResponse(
      (json['dateList'] as List?)?.map((e) => e as String).toList(),
      json['message'] as String?,
      json['status'] as String?,
      json['currenttime'] as String?);
}

slots_response.dart

代码语言:javascript
复制
import 'package:json_annotation/json_annotation.dart';

part 'slots_response.g.dart';

@JsonSerializable(createToJson: false)
class SlotsResponse {
  final String? status;
  final String? message;
  final List<SlotsGroups>? availableGroups;
  SlotsResponse(this.availableGroups, this.message, this.status);

  factory SlotsResponse.fromJson(Map<String, dynamic> map) =>
      _$SlotsResponseFromJson(map);
}

@JsonSerializable(createToJson: false)
class SlotsGroups {
  final String? groupId;
  final String? vendorId;
  final String? groupName;
  final String? status;
  final String? createdAt;
  final String? updatedAt;
  final List<Slots>? availSlots;

  SlotsGroups(this.availSlots, this.createdAt, this.groupId, this.groupName,
      this.status, this.updatedAt, this.vendorId);

  factory SlotsGroups.fromJson(Map<String, dynamic> map) =>
      _$SlotsGroupsFromJson(map);
}

@JsonSerializable(createToJson: false)
class Slots {
  late final String? slotId;
  late final String? startTime;
  late final String? endTime;
  late final String? duration;
  late final String? startDateStrTime;
  late final String? endDateStrTime;
  late final String? stGivenString;
  late final String? endGivenString;
  late final String? status;
  late final String? createdAt;
  late final String? updatedAt;
  late final String? isAnyAppointment;
  late final BookingDetails? bookingDetails;

  Slots(
      this.createdAt,
      this.duration,
      this.endDateStrTime,
      this.endGivenString,
      this.endTime,
      this.slotId,
      this.stGivenString,
      this.bookingDetails,
      this.isAnyAppointment,
      this.startDateStrTime,
      this.startTime,
      this.status,
      this.updatedAt);

  factory Slots.fromJson(Map<String, dynamic> map) => _$SlotsFromJson(map);
}

@JsonSerializable(createToJson: false)
class BookingDetails {
  final String? customerName;
  final String? serviceName;

  BookingDetails(this.customerName, this.serviceName);

  factory BookingDetails.fromJson(Map<String, dynamic> map) =>
      _$BookingDetailsFromJson(map);
}

calendar_response.dart

代码语言:javascript
复制
import 'package:awomowa/responsemodels/appointment/slots/slots_response.dart';
import 'package:json_annotation/json_annotation.dart';

part 'calendar_response.g.dart';

@JsonSerializable(createToJson: false)
class CalenderResponse {
  final String? status;
  final String? message;
  final String? isHavingSlot;
  final String? schedulerId;

  final String? dayInWeek;
  final String? selectedDate;
  final String? createdAt;
  final String? updatedAt;
  final List<Slots>? slotTimings;
  final String? currenttime;
  
  CalenderResponse(
      this.createdAt,
      this.dayInWeek,
      this.isHavingSlot,
      this.message,
      this.schedulerId,
      this.selectedDate,
      this.slotTimings,
      this.status,
      this.updatedAt,
      this.currenttime);

  factory CalenderResponse.fromJson(Map<String, dynamic> map) =>
      _$CalenderResponseFromJson(map);
}

@JsonSerializable(createToJson: false)
class CalenderAvailableDatesResponse {
  final String? status;
  final String? message;
  final List<String>? dateList;
  final String? currenttime;

  CalenderAvailableDatesResponse(this.dateList, this.message, this.status,this.currenttime);
  factory CalenderAvailableDatesResponse.fromJson(Map<String, dynamic> map) =>
      _$CalenderAvailableDatesResponseFromJson(map);
}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-05-04 13:41:11

问题是map中使用的函数可以返回null。换句话说:它的返回类型是Slots?,而不是Slots

要解决这个问题,您可以从列表中筛选空值,将列表从List<Slots?>转换为List<Slots>,如下所示

代码语言:javascript
复制
(json['slotTimings'] as List?)
    ?.map(
        (e) => e == null ? null : Slots.fromJson(e as Map<String, dynamic>))
    .whereType<Slots>().toList(),
票数 1
EN

Stack Overflow用户

发布于 2022-05-04 13:47:01

我猜你在用json_annotationjson_serializablebuild_runner。正如**.g文件中指定的那样。

代码语言:javascript
复制
// GENERATED CODE - DO NOT MODIFY BY HAND

您应该修改模型文件,以适应当前的问题/情况,然后运行:

flutter pub run build_runner build

flutter pub run build_runner build --delete-conflicting-outputs

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72113736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档