2

I am trying to user Dio Client for making API calls. While I receive the response It throws an error

'_InternalLinkedHashMap' is not a subtype of type 'String'

Trying to resolve it but I can't. Below is the code


  Future<dynamic> get(
      String uri, {
        Map<String, dynamic> queryParameters,
        Options options,
        CancelToken cancelToken,
        ProgressCallback onReceiveProgress,
      }) async {
    try {
      final Response response = await _dio.get(
        uri,
        queryParameters: queryParameters,
        options: options,
        cancelToken: cancelToken,
        onReceiveProgress: onReceiveProgress,
      );
      return response.data;
    } catch (e) {
      print(e.toString());
      throw e;
    }
  }
} 

Post Api Call

Future<News> getPosts() async {
    try {
      final res = await _dioClient.get(Endpoints.getPosts);
      return News.fromJson(res);
    } catch (e) {
      print(e.toString());
      throw e;
    }
  }

Model class uses built_value

abstract class News implements Built<News, NewsBuilder> {
  News._();

  factory News([updates(NewsBuilder b)]) = _$News;

  @BuiltValueField(wireName: 'status')
  String get status;
  @BuiltValueField(wireName: 'totalResults')
  int get totalResults;
  @BuiltValueField(wireName: 'articles')
  BuiltList<Articles> get articles;
  String toJson() {
    return json.encode(serializers.serializeWith(News.serializer, this));
  }

  static News fromJson(String jsonString) {

    return serializers.deserializeWith(
        News.serializer, json.decode(jsonString));
  }

  static Serializer<News> get serializer => _$newsSerializer;
}

Serializer class

@SerializersFor([
  News,
  Articles,
  Source,
])
final Serializers serializers = (_$serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build();

help me how to solve it

Anbu
  • 671
  • 1
  • 6
  • 18
  • That error is pointing to a line in your code. Could you point out which line it is pointing to? Otherwise it's too much guess work. – Gazihan Alankus Nov 08 '19 at 06:38
  • how do you provide the query parameters ? – Salma Nov 08 '19 at 08:43
  • @Salma. Please Find the details ```flutter: *** Request *** flutter: uri: https://newsapi.org/v2/top-headlines?country=in&apiKey=a6f53af8e9ac449dac09131e7aeae549 flutter: method: GET flutter: contentType: application/json; charset=utf-8 flutter: responseType: ResponseType.json flutter: followRedirects: true flutter: connectTimeout: 3000 flutter: receiveTimeout: 5000 flutter: extra: {} flutter: header: Content-Type:application/json; charset=utf-8``` – Anbu Nov 08 '19 at 09:40
  • @GazihanAlankus I have debugged the code and the error happens after execting the line ```final res = await _dioClient.get(Endpoints.getPosts); return News.fromJson(res);``` – Anbu Nov 11 '19 at 04:55

1 Answers1

-1

You need to change 2 things

1) use jsonSerializers instead of default serializers in your built_value.

2 )use response.toString() instead of data in dio. Here a working example from me, just copy the parts you need.

Built Value Model

abstract class TravelRequest implements Built<TravelRequest, TravelRequestBuilder> {
  TravelRequest._();

  factory TravelRequest([updates(TravelRequestBuilder b)]) = _$TravelRequest;

  @nullable
  @BuiltValueField(wireName: 'id')
  int get id;

  @nullable
  @BuiltValueField(wireName: 'country_from')
  String get countryFrom;

  @nullable
  @BuiltValueField(wireName: 'traveler_name')
  String get travelerName;

  @nullable
  @BuiltValueField(wireName: 'traveler_email')
  String get travelerEmail;

  @nullable
  @BuiltValueField(wireName: 'description')
  String get description;

  String toJson() {
    return json.encode(jsonSerializers.serializeWith(TravelRequest.serializer, this));
  }

  static TravelRequest fromJson(String jsonString) {
    return jsonSerializers.deserializeWith(TravelRequest.serializer, json.decode(jsonString));
  }

  static Serializer<TravelRequest> get serializer => _$travelRequestSerializer;
}

How to use with Dio

  Future createTravelRequest(TravelRequest travelRequest) {
return _apiProvider.dio.post(rootPath, data: travelRequest.toJson()).then((response) {
  travelRequests.add(TravelRequest.fromJson(response.toString()));
});
  }
Patrick P.
  • 109
  • 8
  • This answer is a little lacking: Where does `jsonSerializers ` come from? There are no references to it in the OP and you've not described the package it comes from or how to get it in the first place. – HBG May 10 '20 at 00:14