flutter 返回类型不能分配给String

xdnvmnnf  于 2023-02-16  发布在  Flutter
关注(0)|答案(1)|浏览(173)

我做了一个图表,数据取自API。但是我有一个问题,变量startDate和truck不能赋值,因为类型不同。有人知道是什么原因吗?
在此附上错误。
变量开始日期错误

可变转向架误差

下面是变量的JSON模型

class GraphicLoad {
  GraphicLoad(
    this.startDate,
    this.excavator,
    this.truck,
  );

  factory GraphicLoad.fromJson(Map<String, dynamic> json) =>
      _$GraphicLoadFromJson(json);

  Map<String, dynamic> toJson() => _$GraphicLoadToJson(this);

  @JsonKey(name: 'start_date')
  final DateTime startDate;

  final String excavator;

  final String truck;
}

在建立JSON模型后,编写函数进行解析

GraphicLoad _$GraphicLoadFromJson(Map<String, dynamic> json) => GraphicLoad(
      DateTime.parse(json['start_date'] as String),
      json['excavator'] as String,
      json['truck'] as String,
    );

Map<String, dynamic> _$GraphicLoadToJson(GraphicLoad instance) =>
    <String, dynamic>{
      'start_date': instance.startDate.toIso8601String(),
      'excavator': instance.excavator,
      'truck': instance.truck,
    };

这是海图的代码

SplineAreaSeries<GraphicLoad, String>(
                                gradient: LinearGradient(
                                  colors: <Color>[
                                    ColorName.brandSecondaryBlue
                                        .withOpacity(0.3),
                                    ColorName.brandPrimaryBlue,
                                  ],
                                  stops: <double>[0.2, 0.7],
                                  begin: Alignment.bottomCenter,
                                  end: Alignment.topCenter,
                                ),
                                dataSource: graphicLoad,
                                xValueMapper: (GraphicLoad data, _) =>
                                    data.startDate, // error in here---------------
                                yValueMapper: (GraphicLoad data, _) =>
                                    data.truck,     // error in here---------------
                                name: 'Trucks',
                                markerSettings: const MarkerSettings(
                                  isVisible: false,
                                  color: ColorName.brandPrimaryBlue,
                                ),
                              ),
szqfcxe2

szqfcxe21#

您在委托函数xValueMapperyValueMapper上返回了错误的类型数据。
xValueMapper需要返回String的函数
yValueMapper需要返回int的函数

SplineAreaSeries<GraphicLoad, String>(
                                gradient: LinearGradient(
                                  colors: <Color>[
                                    ColorName.brandSecondaryBlue
                                        .withOpacity(0.3),
                                    ColorName.brandPrimaryBlue,
                                  ],
                                  stops: <double>[0.2, 0.7],
                                  begin: Alignment.bottomCenter,
                                  end: Alignment.topCenter,
                                ),
                                dataSource: graphicLoad,
                                xValueMapper: (GraphicLoad data, _) =>
                                    DateFormat('d MMM yy').format(data.startDate), 
                                yValueMapper: (GraphicLoad data, _) =>
                                    0,// or use int.parse(data.truck) if trunk is integer     
                                name: 'Trucks',
                                markerSettings: const MarkerSettings(
                                  isVisible: false,
                                  color: ColorName.brandPrimaryBlue,
                                ),
                              ),

相关问题