Open alvi-se opened 3 months ago
As a workaround, could you call your model Article
or something similar?
Yes, probably it's the best workaround to use both classes without conflicts.
Other options are:
import 'package:chopper/chopper.dart' hide Post;
import 'package:chopper/chopper.dart' as chopper;
We're unlikely to change any of the annotation classes (@Get()
, @Post()
, @Put()
, @Patch()
, @Delete()
) becuase that would break the API.
Unfortunately, importing chopper with a prefix gives the same problem: the code is generated using chopper classes without the prefixes. IMHO, the best way to fix this is to make the generator add possible prefixes. This is just an idea, I am a beginner in Dart and I don't know anything about code generation, so I don't know if it's possible to do so.
the best way to fix this is to make the generator add possible prefixes
I'm just looking into that. Not sure about it either. 🙈
I suggest you don't use these reserved class names.
EDIT: Looks like freezed has a similar issue. Could be nasty.
Indeed looks like a tough problem. Have been experimenting a bit with macros but having checked this use case but maybe the dart augmentation offer a workaround. But this solution will have to wait till macros are ready. (And implemented in chopper)
Another workaround would be to rename @Get()
, @Post()
, @Put()
etc. to @GET()
, @POST()
, @PUT()
etc., but that's just kicking the can down the road, not to mention the breaking changes. 🫠
Steps to Reproduce
Post
class.part 'post.g.dart';
@JsonSerializable() class Post { final String id; final String userId; final DateTime timestamp; final String content;
Post({ required this.id, required this.userId, required this.timestamp, required this.content });
factory Post.fromJson(Map<String, dynamic> json) => _$PostFromJson(json); }
dart run build_runner build --delete-conflicting-outputs
. The generated code will be the following:part of 'post_api.dart';
// ** // ChopperGenerator // **
// coverage:ignore-file // ignore_forfile: type=lint final class $PostApi extends PostApi { _$PostApi([ChopperClient? client]) { if (client == null) return; this.client = client; }
@override final Type definitionType = PostApi;
@override Future<List> getPosts() async {
final Uri $url = Uri.parse('/posts');
final Request $request = Request(
'GET',
$url,
client.baseUrl,
);
final Response $response = await client.send<List, Post>($request);
return $response.bodyOrThrow;
}
}
Logs
``` lib/models/post_api.chopper.dart:21:22: Error: The return type of the method '_$PostApi.getPosts' is 'Future>', which does not match the return type, 'Future
>', of the overridden method, 'PostApi.getPosts'. - 'Future' is from 'dart:async'. - 'List' is from 'dart:core'. - 'Post/*1*/' is from 'package:chopper/src/annotations.dart' ('/C:/Users/alvif/AppData/Local/Pub/Cache/hosted/pub.dev/chopper-8.0.1+1/lib/src/annotations.dart'). - 'Post/*2*/' is from 'package:example/models/post.dart' ('lib/models/post.dart'). Change to a subtype of 'Future
>'. Future
> getPosts() async { ^ lib/models/post_api.dart:20:33: Context: This is the overridden method ('getPosts'). Future
> getPosts(); ^ ``` ``` Analyzing example... error • lib\models\post_api.chopper.dart:21:22 • '_$PostApi.getPosts' ('Future
> Function()') isn't a valid override of 'PostApi.getPosts' ('Future
> Function()'). • invalid_override - The member being overridden at lib\models\post_api.chopper.dart:21:17. error • test\example_test.dart:1:8 • Target of URI doesn't exist: 'package:example/example.dart'. Try creating the file referenced by the URI, or try using a URI for a file that does exist. • uri_does_not_exist error • test\example_test.dart:6:12 • The function 'calculate' isn't defined. Try importing the library that defines 'calculate', correcting the name to the name of an existing function, or defining a function named 'calculate'. • undefined_function info • lib\models\post.g.dart:16:22 • The declaration '_$PostToJson' isn't referenced. Try removing the declaration of '_$PostToJson'. • unused_element 4 issues found. ``` ``` Dart SDK version: 3.5.1 (stable) (Tue Aug 13 21:02:17 2024 +0000) on "windows_x64" ```