Hi William Arcaya C.

Thank you for reading our post and for your question.

There are many approaches to make a service call.

In our case, we have written our own wrapper over the http package to make our service call, and 'Mapable', 'apiClient' are part of that wrapper.

Here is a simple approach you can try with the code snippet below:

api_client.dart :

//*********** START OF api_client.dart *************

import 'dart:convert';

import 'package:http/http.dart' as Client;

import 'package:provider_pattern_app/dashboard/http_client.dart';

enum URLType {

Login

}

class APIClient {

String baseURL = 'https://reqres.in/';

Future<dynamic> callPostUrl(URLType type, Map<String, dynamic>data) async{

String endPoint;

String body = json.encode(data);

print('data $data');

switch (type) {

case URLType.Login:

endPoint = baseURL + 'api/login';

break;

default:

print(default);

}

var response = await Client.post(endPoint, headers: {"Content-Type": "application/json"}, body: body);

String jsonsDataString = response.body.toString();

print('json string ${jsonsDataString}');

if (response.statusCode == 200) {

var data = json.decode(jsonsDataString);

return Future.value(data);

}

}

}

APIClient apiClient = APIClient();

//*************END OF api_client.dart. *****************

login_response.dart :

//*********** START OF login_response.dart. *****************

class LoginResponse {

String token;

LoginResponse(Map<String, dynamic> data) {

token = data['token'];

}

}

//*************END OF login_response.dart

In login_provider.dart, replace the submitLogin() method with the following:

Future submitLogin() async {

setState(ViewState.Busy);

final response = await apiClientt.callPostUrl(URLType.Login, {'email': _email.value, 'password': _password.value});

var user = LoginResponse.fromJson(response);

setState(ViewState.Idle);

return user;

}

Please note - For our login, we have used this URL: http://www.reqres.in/

This is a dummy URL for practice.

We hope this helps.

Cheers and stay safe :)

Written by

DLT Labs is a global leader in Distributed Ledger Technology and Enterprise Products. To know more, head over to: https://www.dltlabs.com/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store