Вход в Flutter с использованием APIAndroid

Форум для тех, кто программирует под Android
Ответить
Anonymous
 Вход в Flutter с использованием API

Сообщение Anonymous »

Я пытаюсь использовать API для экрана входа в систему, но продолжаю получать эту ошибку.
Я хочу иметь возможность войти через API в свое приложение Flutter.
Первая часть — это то, что есть отправляется в API, а вторая часть — это ответ сервера, как показано в коде ниже:
I/flutter ( 892): {email: rashid.david12@gmail.com, password: 123456789}
I/flutter ( 892): {errors: [The email field is required., The password field is required.]}

Это мой код, что мне делать или какой-нибудь обходной путь для описанной выше ошибки?
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 {
// 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: [
// 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: [
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,
),
],
),
// ],
),
// ),
);
}
}


Подробнее здесь: https://stackoverflow.com/questions/666 ... ing-an-api
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Android»