json_serializable what am I overlooking?

Viewed 44

I'm having trouble with jsonEncode even though I've done this multiple times and followed the documentation (Link). I'm not seeing what is wrong.

import 'package:json_annotation/json_annotation.dart';

part 'block.g.dart';

@JsonSerializable()
class Block {
  Block(
      this.name,
      this.required,
      this.type,
      this.stringValue,
      this.checkboxValue,
      this.numericValue,
      this.minNumericRestraint,
      this.maxNumericRestraint
      );

  String name;
  bool required;
  String type;
  String stringValue;
  bool checkboxValue;
  double numericValue;
  double minNumericRestraint;
  double maxNumericRestraint;



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

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

}
// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'block.dart';

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

Block _$BlockFromJson(Map<String, dynamic> json) => Block(
      json['name'] as String,
      json['required'] as bool,
      json['type'] as String,
      json['stringValue'] as String,
      json['checkboxValue'] as bool,
      (json['numericValue'] as num).toDouble(),
      (json['minNumericRestraint'] as num).toDouble(),
      (json['maxNumericRestraint'] as num).toDouble(),
    );

Map<String, dynamic> _$BlockToJson(Block instance) => <String, dynamic>{
      'name': instance.name,
      'required': instance.required,
      'type': instance.type,
      'stringValue': instance.stringValue,
      'checkboxValue': instance.checkboxValue,
      'numericValue': instance.numericValue,
      'minNumericRestraint': instance.minNumericRestraint,
      'maxNumericRestraint': instance.maxNumericRestraint,
    }

Calling jsonEncode(Block(...)); results in following error: Error: Converting object to an encodable object failed: Instance of 'Block'

I reran the build script multiple times, wrote the toJson() method manually and invalidated my Android Studio cache.

0 Answers
Related