Home > Software engineering >  why i got on error on flutter dio implementation?
why i got on error on flutter dio implementation?

Time:09-02

can someone help me to check why my code is error ? i already make user freeze, network freeze. as we can see i try to use all dio package. i got more code on bellow. in here i give u image to show what error. i want to try to make flutter dio easier so i dont need to write network response multiple times. this is the error

import 'package:dio/dio.dart';
import 'package:latihan_dio/dio_interceptor.dart';
import 'package:latihan_dio/src/features/home/domain/network_response.dart';

import '../../../../dio_client.dart';
import '/src/features/home/domain/user.dart';

enum RequestType { GET, POST, PUT, PATCH, DELETE }

class DioClient {
  final dio = createDio();

  DioClient._internal();

  static final _singleton = DioClient._internal();

  factory DioClient() => _singleton;

  static Dio createDio() {
    var dio = Dio(BaseOptions(
      baseUrl: "https://reqres.in/api/users?page=2",
      receiveTimeout: 20000, // 20 seconds
      connectTimeout: 20000,
      sendTimeout: 20000,
    ));

    // dio.interceptors.addAll({
    //   AuthInterceptor(dio),
    // });
    dio.interceptors.addAll({
      Logging(dio),
    });

    return dio;
  }

  Future<NetworkResponse?> apiCall({
    required String url,
    required RequestType requestType,
    Map<String, dynamic>? queryParameters,
    Map<String, dynamic>? body,
    Map<String, String>? header,
    RequestOptions? requestOptions,
  }) async {
    late Response result;
    // try {
    switch (requestType) {
      case RequestType.GET:
        {
          Options options = Options(headers: header);
          result = await dio.get(url,
              queryParameters: queryParameters, options: options);
          break;
        }
      case RequestType.POST:
        {
          Options options = Options(headers: header);
          result = await dio.post(url, data: body, options: options);
          break;
        }
      case RequestType.DELETE:
        {
          Options options = Options(headers: header);
          result =
              await dio.delete(url, data: queryParameters, options: options);
          break;
        }
      case RequestType.PUT:
        {
          Options options = Options(headers: header);
          result = await dio.put(url, data: body, options: options);
          break;
        }
      case RequestType.PATCH:
        {
          Options options = Options(headers: header);
          result = await dio.patch(url, data: body, options: options);
          break;
        }
        
    }
          if(result != null) {
        return NetworkResponse.success(result.data);
      } else {
        return const NetworkResponse.error("Data is null");
      }
    } on DioError catch (error) {
      return NetworkResponse.error(error.message);
    } catch (error) {
      return NetworkResponse.error(error.toString());
  }

}

but I don't think it has anything to do with the error above

CodePudding user response:

I think because you made try as a comment.

You have written // try {.

Remove //

  • Related