import 'dart:async'; import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'package:local_spend/common/functions/save_current_login.dart'; import 'package:local_spend/common/functions/show_dialog_single_button.dart'; import 'package:local_spend/model/json/login_model.dart'; Future _incorrectDialog(BuildContext context) async { return showDialog( context: context, barrierDismissible: true, builder: (BuildContext context) { return AlertDialog( title: Text("Unable to Login"), content: Text("Incorrect login details. Please try again."), actions: [ FlatButton( child: Text('OK'), onPressed: () { Navigator.of(context).pop(); }, ), ], ); }, ); } Future requestLoginAPI( BuildContext context, String email, String password) async { //var apiUrl = ConfigWrapper.of(context).apiKey; final url = "https://dev.peartrade.org/api/login"; Map body = { 'email': email, 'password': password, }; // debugPrint('$body'); final response = await http.post( url, body: json.encode(body), ); // debugPrint(response.body); if (response.statusCode == 200) { final responseJson = json.decode(response.body); var user = new LoginModel.fromJson(responseJson); saveCurrentLogin(responseJson, body["email"]); Navigator.of(context).pushReplacementNamed('/HomePage'); return LoginModel.fromJson(responseJson); } else { // debugPrint("Invalid, either credentials are wrong or server is down"); final responseJson = json.decode(response.body); saveCurrentLogin(responseJson, body["email"]); _incorrectDialog(context); return null; } }