我的http post
确实有问题,我试图发布一个注册表单TextField,但是我一直对此有很多问题。现在,我在使用提供程序,这是我的代码:>
import 'dart:convert';
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:logistics_app/models/registerApi.dart';
import 'package:logistics_app/ui/dashboard_view.dart';
import 'package:logistics_app/utils/validation.dart';
import 'package:http/http.dart' as http;
import '../ui/login_view.dart';
class UserOnboardingModel extends ChangeNotifier with ValidationMixin{
TextEditingController firstController = TextEditingController();
TextEditingController lastController = TextEditingController();
TextEditingController passwordController = TextEditingController();
TextEditingController emailController = TextEditingController();
TextEditingController phoneController = TextEditingController();
bool showPassword = true;
final formKey = GlobalKey<FormState>();
Future<RegisterModel> _futureRegister;
Future<RegisterModel> createRegister()async{;
final http.Response response = await http.post(
"apiKey",
headers: {'content-type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Some token'
},
body: jsonEncode(<String, String>{
'firstName' : validateFirstName(firstController.text),
'lastName' : validateLastName(lastController.text),
'emailAddress' : validateEmail(emailController.text),
'phoneNumber' : validatePhoneNumber(phoneController.text),
'password' : validatePassword(passwordController.text)
})
);
if(response.statusCode < 200 || response.statusCode >= 400){
print(json.decode(response.body));
return RegisterModel.fromJson(json.decode(response.body));
}else{
print(response.statusCode);
throw Exception('Failed to create Register');
}
}
void passRegister(){
_futureRegister = createRegister();
}
void registerForm(){
FutureBuilder<RegisterModel>(
future: _futureRegister,
builder: (context, snapshot){
if(snapshot.hasData){
return Column(
children: [
Text(snapshot.data.firstName),
Text(snapshot.data.lastName),
Text(snapshot.data.emailAddress),
Text(snapshot.data.phoneNumber),
Text(snapshot.data.password)
],
);
}else if(snapshot.hasError){
return Text("${snapshot.error}");
}
return CircularProgressIndicator();
},
);
}
void togglePassword(){
showPassword = !showPassword;
notifyListeners();
}
void validateForm(){
formKey.currentState.validate();
}
void moveToLogin(context){
//go to login page
if(formKey.currentState.validate()){
Navigator.pushNamed(context, LogIn.LOG_IN_ROUTE);
}
else{
Fluttertoast.showToast(
msg: "Please Fill The Form",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 5,
backgroundColor: Colors.black,
);
}
}
void moveToDashBoardView(context){
//go to login page
if(formKey.currentState.validate()){
Navigator.push(context, MaterialPageRoute(
builder: (context) => DashboardView()
));
}
else{
Fluttertoast.showToast(
msg: "Please Fill The Form",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 5,
backgroundColor: Colors.black,
);
}
}
void moveToSignup(){
//go to sign up page
notifyListeners();
}
void moveToForgottenPassword(){
//go to forgotten password page
notifyListeners();
}
}
这不是错误,因为cos试图将用户输入的表单发布到apiKey
上,但是有错误
这是错误:
W/IInputConnectionWrapper(13401): beginBatchEdit on inactive InputConnection
W/IInputConnectionWrapper(13401): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper(13401): getTextAfterCursor on inactive InputConnection
W/IInputConnectionWrapper(13401): getSelectedText on inactive InputConnection
W/IInputConnectionWrapper(13401): endBatchEdit on inactive InputConnection
I/flutter (13401): {type: https://tools.ietf.org/html/rfc7231#section-6.5.1, title: One or more validation errors occurred., status: 400, traceId: |413e7730-4dae4ee449b12286., errors: {LastName: ['Last Name' must not be empty.], FirstName: ['First Name' must not be empty.], PhoneNumber: ['Phone Number' must not be empty.], EmailAddress: ['Email Address' must not be empty.]}}