skip to Main Content

I have a long string of around 1042 characters stored in the database, I tried hitting API to retrieve this string, when I ran it in Postman it worked, but when I hit request in flutter the application suddenly stopped and froze.

This is the code:

  @override
  Future<ResponseAPI> getPrivacyPolicy() async {
    try {
      Dio http = await dio();
      Response response = await http.get("/setting/type", queryParameters: {
        "type": 7,
      });

      final data = response.data;
      return ResponseAPI.fromJson(data);
    } on DioException catch (e) {
      if (e.response?.statusCode == 403) {
        return ResponseAPI(
          message: e.response?.data['message'],
          statusCode: 403,
        );
      } else {
        return const ResponseAPI(
          message: "There's something wrong, please try again later",
          statusCode: 500,
        );
      }
    } catch (e) {
      return const ResponseAPI(
        message: "There's something wrong, please try again later",
        statusCode: 500,
      );
    }
  }

and here’s the response in Postman:

enter image description here

2

Answers


  1. Try defining responseType for the Dio request Options

      options: Options(
          method: 'GET',
          responseType: ResponseType.plain,
       ),
    

    Also, check for any exception thrown on the runtime environment or by using try and catch

    Login or Signup to reply.
  2. First you need to make a model

    class SettingModel {
       String title;
       String content;
       InstitutionIdModel institution;
    
       SettingModel(this.title, this.content, this.institution);
    
       factory SettingModel.fromJson(Map<String, dynamic> json) => SettingModel(
             json['title'],
             json['content'],
             json['institution'],
           );
    }
    
    class InstitutionIdModel {
       int id;
       String name;
       String email;
       String? level;
    
       InstitutionIdModel(this.id, this.name, this.email, this.level);
    
       factory InstitutionIdModel.fromJson(Map<String, dynamic> json) =>
           InstitutionIdModel(
             json['id'] as int,
             json['name'],
             json['email'],
             json['level'],
           );
    }
    

    Then you have to make a model as follows

    Dio http = await dio();
    Response response = await http.get("/setting/type", queryParameters: {
    "type": 7,
    });
    
    
    return response.data['responseData'].map<SettinModel>((json)=>SettinModel.fromJson(data)).toList();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search