You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class Token {
Token(this.token);
String? token;
factory Token.fromJson(Map<String, String?> data) {
if (data
case {
'token': String token,
}) {
return Token(token);
} else {
if (data['token'] is! String) {
throw FormatException('Invalid JSON: required "token" field of type String in $data');
}
throw FormatException('Invalid JSON: $data');
}
}
Map<String, String?> toJson() {
// return a map literal with all the non-null key-value pairs
return {
'token': token,
};
}
}
Or like this:
import 'package:json_annotation/json_annotation.dart';
part 'token.g.dart';
@JsonSerializable()
class Token {
Token(this.token);
final String? token;
factory Token.fromJson(Map<String, dynamic> json) => _$TokenFromJson(json);
Map<String, dynamic> toJson() => _$TokenToJson(this);
}
service:
import 'package:data/models/Token.dart';
import 'package:dio/dio.dart';
import 'package:retrofit/retrofit.dart';
part '***.g.dart';
@RestApi(baseUrl: "https://dev-api.***.com")
abstract class AppRetrofitService {
// helper methods that help you instanciate your service
factory AppRetrofitService(Dio dio, {String baseUrl}) = _AppRetrofitService;
@POST("/api/v1/login")
Future<Token> login(@Body() Map<String, String> body);
}
To Reproduce
Steps to reproduce the behavior:
Run fvm dart run build_runner build --delete-conflicting-outputs or dart run build_runner build --delete-conflicting-outputs depends on do you use fvm or not.
Expected behavior
Successfuly generated class.
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered:
Describe the bug
I am getting this error when I try to generate a retrofit service.
yaml:
model:
service:
To Reproduce
Steps to reproduce the behavior:
Run
fvm dart run build_runner build --delete-conflicting-outputs
ordart run build_runner build --delete-conflicting-outputs
depends on do you use fvm or not.Expected behavior
Successfuly generated class.
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: