Annotation must be either a const variable reference or const constructor invocation
Asked Answered
B

1

5

i had a error; Annotation must be either a const variable reference or const constructor invocation.dart(invalid_annotation) The name 'Headers' is defined in the libraries 'package:dio/src/headers.dart (via package:dio/dio.dart)' and 'package:retrofit/http.dart'. Try using 'as prefix' for one of the import directives, or hiding the name from all but one of the imports.

I want to pass a jwt token. But i cant pass it through Headers because i get errors

// import 'dart:convert';
// import 'dart:io';
// import 'package:amc_new/model/client_amc.dart';
// import 'package:http/http.dart' as http;
// import 'package:flutter_config/flutter_config.dart';

// String uri = FlutterConfig.get('API_URL');

// class ClientAmcService {
//   // ignore: missing_return
//   Future<ClientAmc> getclientAmc(String amcNo) async {
//     try {
//       var response = await http.get(
//         uri + '/report/getamcreport/$amcNo',
//         headers: {
//           HttpHeaders.authorizationHeader: 'jwt',
//         },
//       );
//       print("------------------------------------------------");
//       if (response.statusCode == 200) {
//         print(response.body);
//         print(response.statusCode);
//         print("------------------------------------------------");
//         List<ClientAmc> clientAmcFromJson(String str) => List<ClientAmc>.from(
//             json.decode(str).map((x) => ClientAmc.fromJson(x)));
//         List<ClientAmc> clientamclist = clientAmcFromJson(response.body);
//         return clientamclist[0];
//       } else {
//         print("Not Found");
//       }
//     } catch (e) {
//       print(e.toString());
//     }
//   }
// }

import 'package:amc_new/model/client_amc.dart';
import 'package:dio/dio.dart';
import 'package:retrofit/http.dart';

part 'amc_client_service.g.dart';

@RestApi()
abstract class ClientAmcService {
  factory ClientAmcService(Dio dio, {String baseUrl}) = _ClientAmcService;

  @GET('/report/getamcreport/{amcNo}')
  @Headers(<String, dynamic>{
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'Authorization': 'Bearer {jwt}',
  })
  Future<ClientAmc> getclientAmc(@Path('amcNo') String amcNo);
}
Banns answered 14/5, 2021 at 8:31 Comment(1)
You can use as when importing a package to ensure that all members of that package does get a prefix. So in your case, you can do: import 'package:dio/dio.dart' as dio; which will make it so you need to prefix with dio. when you want to use something from the dio package. This will also ensure that Dart can clearly understand what class you want if multiple packages contains the same class.Magree
A
17

I solved 'hide'. https://dart.dev/guides/language/extension-methods#api-conflicts

import 'package:dio/dio.dart' hide Headers;
Akanke answered 23/6, 2021 at 18:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.