Last active
January 24, 2021 15:44
-
-
Save CoderJava/1c63dd1f42602093fb26bb29dd17704e to your computer and use it in GitHub Desktop.
dio_helper.dart flutter crub cubit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:dartz/dartz.dart'; | |
import 'package:dio/dio.dart'; | |
import 'package:flutter_crud_cubit/model/profile_data.dart'; | |
class DioHelper { | |
Dio _dio; | |
DioHelper() { | |
_dio = Dio( | |
BaseOptions( | |
baseUrl: 'http://api.bengkelrobot.net:8001/api', | |
), | |
); | |
_dio.interceptors.add(LogInterceptor(requestBody: true, responseBody: true)); | |
} | |
Future<Either<String, List<ProfileData>>> getAllProfiles() async { | |
try { | |
var response = await _dio.get('/profile'); | |
var listProfileData = List<ProfileData>.from(response.data.map((e) => ProfileData.fromJson(e))); | |
return Right(listProfileData); | |
} on DioError catch (error) { | |
return Left('$error'); | |
} | |
} | |
Future<Either<String, bool>> addProfile(ProfileData profileData) async { | |
try { | |
await _dio.post( | |
'/profile', | |
data: profileData.toJson(), | |
); | |
return Right(true); | |
} on DioError catch (error) { | |
return Left('$error'); | |
} | |
} | |
Future<Either<String, bool>> editProfile(ProfileData profileData) async { | |
try { | |
await _dio.put( | |
'/profile/${profileData.id}', | |
data: profileData.toJson(), | |
); | |
return Right(true); | |
} on DioError catch (error) { | |
return Left('$error'); | |
} | |
} | |
Future<Either<String, bool>> deleteProfile(int id) async { | |
try { | |
await _dio.delete( | |
'/profile/$id', | |
); | |
return Right(true); | |
} on DioError catch (error) { | |
return Left('$error'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment