I'm trying to consume an API for the login screen but keep getting this error. I want to be able to login via an API to my flutter app. the first part is what is being posted to the API and the second part is the server response as seen on the code below
I/flutter ( 892): {email: rashid.david12@gmail.com, password: 123456789}
I/flutter ( 892): {errors: [The email field is required., The password field is required.]}
This is my code, what should I do, or any workaround for the error above?
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class Signin extends StatefulWidget {
@override
_SigninState createState() => _SigninState();
}
class _SigninState extends State<Signin> {
// For CircularProgressIndicator.
bool visible = false ;
// Getting value from TextField widget.
final emailController = TextEditingController();
final passwordController = TextEditingController();
Future userLogin() async{
// Showing CircularProgressIndicator.
setState(() {
visible = true ;
});
// Getting value from Controller
String email = emailController.text;
String password = passwordController.text;
// SERVER LOGIN API URL
var url = 'http://192.236.160.238/api/login';
// Store all data with Param Name.
var data = {'email': email, 'password' : password};
// Starting Web API Call.
var response = await http.post(url, body: json.encode(data[email]));
print(data);
// Getting Server response into variable.
var message = jsonDecode(response.body);
print(message);
// If the Response Message is Matched.
if(message == 'success')
{
// Hiding the CircularProgressIndicator.
setState(() {
visible = false;
});
// Navigate to Home & Sending Email to Next Screen.
Navigator.pushNamed(context, '/home');
}else{
// If Email or Password did not Matched.
// Hiding the CircularProgressIndicator.
setState(() {
visible = false;
});
// Showing Alert Dialog with Response JSON Message.
// showDialog(
// context: context,
// builder: (BuildContext context) {
// return AlertDialog(
// title: new Text(message),
// actions: <Widget>[
// FlatButton(
// child: new Text("OK"),
// onPressed: () {
// Navigator.of(context).pop();
// },
// ),
// ],
// );
// },
// );
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: Icon(Icons.menu),
title: const Text('Bdm Self Service App'),
centerTitle: true,
actions: [
Padding(
padding: EdgeInsets.symmetric(horizontal: 16),
child: Icon(Icons.search),
),
],
backgroundColor: Colors.blue,
elevation: 0.0,
),
body: Padding(
padding: EdgeInsets.fromLTRB(30.0, 40.0, 30.0, 0.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Center(
child: CircleAvatar(
backgroundImage: AssetImage('assets/rcmsbg.png'),
radius: 60.0,
),
),
SizedBox(height: 10.0),
TextFormField(
controller: emailController,
decoration: InputDecoration(labelText: 'Enter Username or Email'),
),
SizedBox(height: 10.0),
TextFormField(
controller: passwordController,
decoration: InputDecoration(labelText: 'Enter Password'),
autofocus: false,
obscureText: true,
),
SizedBox(height: 20.0),
FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(color: Colors.green)),
onPressed: userLogin,
child: Text('login'),
color: Colors.green,
minWidth: 300.0,
),
SizedBox(height: 20.0),
Text('Forgot Password?'),
FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(color: Colors.green)),
onPressed: () {
Navigator.pushNamed(context, '/resetpassword');
},
child: Text('Reset Password'),
color: Colors.green,
minWidth: 200.0,
),
SizedBox(height: 20.0),
Text('Do not have an account yet?'),
FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(color: Colors.green)),
onPressed: () {
Navigator.pushNamed(context, '/signup');
},
child: Text('Sign Up'),
color: Colors.green,
minWidth: 200.0,
),
],
),
// ],
),
// ),
);
}
}