Я был довольно плохо знаком с трепетом. Мне было поручено отправить электронное письмо, имя и пароль для указанной мне ссылки для регистрации. Ответ на ответ почтальона при отправке запроса
Ссылка PHP код для регистрации ссылка
index.php
/**
* User Registration
* url - /register
* method - POST
* params - name, email, password
*/
$app->post('/register', function() use ($app) {
// check for required params
verifyRequiredParams(array('name', 'email', 'password'));
$response = array();
// reading post params
$name = $app->request->post('name');
$email = $app->request->post('email');
$password = $app->request->post('password');
// validating email address
validateEmail($email);
$db = new DbHandler();
$res = $db->createUser($name, $email, $password);
if ($res == USER_CREATED_SUCCESSFULLY) {
$response["error"] = false;
$response["message"] = "You are successfully registered";
echoRespnse(201, $response);
} else if ($res == USER_CREATE_FAILED) {
$response["error"] = true;
$response["message"] = "Oops! An error occurred while registereing";
echoRespnse(200, $response);
} else if ($res == USER_ALREADY_EXISTED) {
$response["error"] = true;
$response["message"] = "Sorry, this email already existed";
echoRespnse(200, $response);
}
});
Код флаттера для отправки данных is
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'newScreen.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('User Registration Form')),
body: Center(
child: RegisterUser()
)
)
);
}
}
class RegisterUser extends StatefulWidget {
RegisterUserState createState() => RegisterUserState();
}
class RegisterUserState extends State {
// Boolean variable for CircularProgressIndicator.
bool visible = false ;
// Getting value from TextField widget.
final nameController = TextEditingController();
final emailController = TextEditingController();
final passwordController = TextEditingController();
Future userRegistration() async{
// Showing CircularProgressIndicator.
setState(() {
visible = true ;
});
// Getting value from Controller
String name = nameController.text;
String email = emailController.text;
String password = passwordController.text;
// SERVER API URL
var url = 'http://cafeliant.com/android-login/signupApi.php';
var headers = {
'content-type': 'application/json'
};
// Store all data with Param Name.
var data = {'name': name, 'email': email, 'password' : password};
// Starting Web API Call.
var response = await http.post(url, body: json.encode(data),);
// Getting Server response into variable.
var message = jsonDecode(response.body);
print(message);
// If Web call Success than Hide the CircularProgressIndicator.
if(response.statusCode == 200){
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).push(MaterialPageRoute(builder: (context) => newS(),));
},
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Center(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(12.0),
child: Text('User Registration Form',
style: TextStyle(fontSize: 21))),
Divider(),
Container(
width: 280,
padding: EdgeInsets.all(10.0),
child: TextField(
controller: nameController,
autocorrect: true,
decoration: InputDecoration(hintText: 'Enter Your Name Here'),
)
),
Container(
width: 280,
padding: EdgeInsets.all(10.0),
child: TextField(
controller: emailController,
autocorrect: true,
decoration: InputDecoration(hintText: 'Enter Your Email Here'),
)
),
Container(
width: 280,
padding: EdgeInsets.all(10.0),
child: TextField(
controller: passwordController,
autocorrect: true,
obscureText: true,
decoration: InputDecoration(hintText: 'Enter Your Password Here'),
)
),
RaisedButton(
onPressed: userRegistration,
color: Colors.green,
textColor: Colors.white,
padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
child: Text('Click Here To Register User Online'),
),
Visibility(
visible: visible,
child: Container(
margin: EdgeInsets.only(bottom: 30),
child: CircularProgressIndicator()
)
),
],
),
)));
}
}
При отправке ответов из приложения мои запросы не выполняются, и при печати ответа я получаю ответ по умолчанию, который говорит, что я не отправляю никаких запросов.