trevorwang / retrofit.dart

retrofit.dart is an dio client generator using source_gen and inspired by Chopper and Retrofit.
https://mings.in/retrofit.dart/
MIT License
1.08k stars 251 forks source link

The getter 'MediaType' isn't defined for the class #692

Open sbatezat opened 5 months ago

sbatezat commented 5 months ago

Describe the bug Bumping retrofit_generator from 8.1.0 to 8.1.1 leads to a generation error.

The getter 'MediaType' isn't defined for the class
Try correcting the name to the name of an existing getter, or defining a getter or field named 'MediaType'.
        contentType: MediaType.parse('application/json'),

To Reproduce Steps to reproduce the behavior:

  @POST("/my/endpoint")
  Future<MyDto> post(
    @Part(name: "data", contentType: "application/json") MyDto data, {
    @Part() File? file,
  });

Expected behavior Generator succeed

dzsonni commented 4 months ago

@sbatezat will take a look at it in a few hours

trevorwang commented 4 months ago

@sbatezat Import MediaType manually as a workaround

sbatezat commented 4 months ago

Thanks, it's working by importing manually http_parser.

import 'package:http_parser/http_parser.dart';

It's a workaround, as you said, so I keep this issue open waiting for a robust fix!

dzsonni commented 4 months ago

@sbatezat The problem here is that MediaType is not imported by default if you are not using it in the template for the api, since we can't add imports in part files, the only solution I see is to expose http_parser's MediaType through export 'package:http_parser/http_parser.dart' show MediaType in http.dart, as that is always imported in template files. Is this acceptable? It would mean an extra dependency. I don't see any other way we could guarantee the import, but I am not an ace when it comes to working with code_gen

TheHazeEffect commented 1 week ago

DioMediaType is an Alias of MediaType in the Dio Package. Maybe this is what we should be generating instead. I would expect everyone having Dio as a dependency

image