elliotching
elliotching

Reputation: 1102

Retrofit Flutter does not generate model-json conversion method

I am following this official guide straight forward and it is obviously not generating _$TaskFromJson and _$TaskToJson methods ,

I've put it in 'task.dart' file and modified part as followed

task.dart

import 'package:json_annotation/json_annotation.dart';
import 'package:retrofit/retrofit.dart';
import 'package:dio/dio.dart';

part 'task.g.dart';

@RestApi(baseUrl: "https://5d42a6e2bc64f90014a56ca0.mockapi.io/api/v1/")
abstract class RestClient {
  factory RestClient(Dio dio, {String baseUrl}) = _RestClient;

  @GET("/tasks")
  Future<List<Task>> getTasks();
}

@JsonSerializable()
class Task {
  String id;
  String name;
  String avatar;
  String createdAt;

  Task(this.id, this.name, this.avatar, this.createdAt);

  factory Task.fromJson(Map<String, dynamic> json) => _$TaskFromJson(json);
  Map<String, dynamic> toJson() => _$TaskToJson(this);
}

and I run this command flutter pub run build_runner build
but the generated part is as followed only without any _$TaskFromJson nor _$TaskToJson

task.g.dart

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'task.dart';

// **************************************************************************
// RetrofitGenerator
// **************************************************************************

class _RestClient implements RestClient {
  _RestClient(this._dio, {this.baseUrl}) {
    ArgumentError.checkNotNull(_dio, '_dio');
    this.baseUrl ??= 'https://5d42a6e2bc64f90014a56ca0.mockapi.io/api/v1/';
  }

  final Dio _dio;

  String baseUrl;

  @override
  getTasks() async {
    const _extra = <String, dynamic>{};
    final queryParameters = <String, dynamic>{};
    final _data = <String, dynamic>{};
    final Response<List<dynamic>> _result = await _dio.request('/tasks',
        queryParameters: queryParameters,
        options: RequestOptions(
            method: 'GET',
            headers: <String, dynamic>{},
            extra: _extra,
            baseUrl: baseUrl),
        data: _data);
    var value = _result.data
        .map((dynamic i) => Task.fromJson(i as Map<String, dynamic>))
        .toList();
    return Future.value(value);
  }
}

I've read this answer but not helping me as this question is talking about cannot generate at all, but mine is generate well with missing JsonSerialized methods. Somemore the answer suggested use Uppercase User filename which is definitely not working.

Upvotes: 0

Views: 3444

Answers (2)

Nischal Karanjit
Nischal Karanjit

Reputation: 1

dependencies: flutter: sdk: flutter cupertino_icons: ^1.0.2 logger: any retrofit: any json_annotation: any

dev_dependencies: flutter_test: sdk: flutter build_runner: any retrofit_generator: any json_serializable: any

Use this version of package of all retrofit associated package Surely it will work

Upvotes: 0

Owczar
Owczar

Reputation: 2593

json_serializable is responsible for generating those methods. Just import it into your pubspec dev_dependencies section, run flutter pub get and flutter pub run build_runner build.

Upvotes: 1

Related Questions