当我想登录我的flutter应用程序时,我遇到了问题。当您输入您的电子邮件和密码,然后点击登录按钮,它只处理加载,但不能去到主页.在Android Studio中,出现了一个错误,就像我发送的图像一样。
this is a picture of the running error
API_service.dart
import 'package:http/http.dart' as http;
import 'dart:convert';
import '../model/login_model.dart';
class APIService {
Future<LoginResponseModel> login(LoginRequestModel requestModel) async {
String url = "http://192.168.100.211:8080/login";
final response = await http.post(url, body: requestModel.toJson());
if (response.statusCode == 200 || response.statusCode == 400) {
return LoginResponseModel.fromJson(
json.decode(response.body),
);
} else {
throw Exception('Failed to load data!');
}
}
}
Login_model.dart
class LoginResponseModel {
final String token;
final String error;
LoginResponseModel({this.token, this.error});
factory LoginResponseModel.fromJson(Map<String, dynamic> json) {
return LoginResponseModel(
token: json["token"] != null ? json["token"] : "",
error: json["error"] != null ? json["error"] : "",
);
}
}
class LoginRequestModel {
String email;
String password;
LoginRequestModel({
this.email,
this.password,
});
Map<String, dynamic> toJson() {
Map<String, dynamic> map = {
'email': email.trim(),
'password': password.trim(),
};
return map;
}
}
登录_页面.dart
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_crud_api_sample_app/beranda/beranda_view.dart';
import '/api/api_service.dart';
import '/model/login_model.dart';
import '../ProgressHUD.dart';
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
bool hidePassword = true;
bool isApiCallProcess = false;
GlobalKey<FormState> globalFormKey = GlobalKey<FormState>();
LoginRequestModel loginRequestModel;
final scaffoldKey = GlobalKey<ScaffoldState>();
@override
void initState() {
super.initState();
loginRequestModel = new LoginRequestModel();
}
@override
Widget build(BuildContext context) {
return ProgressHUD(
child: _uiSetup(context),
inAsyncCall: isApiCallProcess,
opacity: 0.3,
);
}
Widget _uiSetup(BuildContext context) {
return Scaffold(
key: scaffoldKey,
backgroundColor: Theme.of(context).accentColor,
body: SingleChildScrollView(
child: Column(
children: <Widget>[
Stack(
children: <Widget>[
Container(
width: double.infinity,
padding: EdgeInsets.symmetric(vertical: 30, horizontal: 20),
margin: EdgeInsets.symmetric(vertical: 85, horizontal: 20),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Theme.of(context).primaryColor,
boxShadow: [
BoxShadow(
color: Theme.of(context).hintColor.withOpacity(0.2),
offset: Offset(0, 10),
blurRadius: 20)
],
),
child: Form(
key: globalFormKey,
child: Column(
children: <Widget>[
SizedBox(height: 25),
Text(
"Login",
style: Theme.of(context).textTheme.headline2,
),
SizedBox(height: 20),
new TextFormField(
keyboardType: TextInputType.emailAddress,
onSaved: (input) => loginRequestModel.email = input,
validator: (input) => !input.contains('@')
? "Email Id should be valid"
: null,
decoration: new InputDecoration(
hintText: "Email Address",
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context)
.accentColor
.withOpacity(0.2))),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).accentColor)),
prefixIcon: Icon(
Icons.email,
color: Theme.of(context).accentColor,
),
),
),
SizedBox(height: 20),
new TextFormField(
style:
TextStyle(color: Theme.of(context).accentColor),
keyboardType: TextInputType.text,
onSaved: (input) =>
loginRequestModel.password = input,
validator: (input) => input.length < 3
? "Password should be more than 3 characters"
: null,
obscureText: hidePassword,
decoration: new InputDecoration(
hintText: "Password",
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context)
.accentColor
.withOpacity(0.2))),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).accentColor)),
prefixIcon: Icon(
Icons.lock,
color: Theme.of(context).accentColor,
),
suffixIcon: IconButton(
onPressed: () {
setState(() {
hidePassword = !hidePassword;
});
},
color: Theme.of(context)
.accentColor
.withOpacity(0.4),
icon: Icon(hidePassword
? Icons.visibility_off
: Icons.visibility),
),
),
),
SizedBox(height: 30),
FlatButton(
padding: EdgeInsets.symmetric(
vertical: 12, horizontal: 80),
onPressed: () {
if (validateAndSave()) {
print(loginRequestModel.toJson());
setState(() {
isApiCallProcess = true;
});
APIService apiService = new APIService();
apiService.login(loginRequestModel).then((value) {
if (value != null) {
setState(() {
isApiCallProcess = false;
});
if (value.token.isNotEmpty) {
final snackBar = SnackBar(
content: Text("Login Successful"));
scaffoldKey.currentState
.showSnackBar(snackBar);
} else {
final snackBar =
SnackBar(content: Text(value.error));
scaffoldKey.currentState
.showSnackBar(snackBar);
}
}
});
}
},
child: Text(
"Login",
style: TextStyle(color: Colors.white),
),
color: Theme.of(context).accentColor,
shape: StadiumBorder(),
),
SizedBox(height: 15),
],
),
),
),
],
),
],
),
),
);
}
bool validateAndSave() {
final form = globalFormKey.currentState;
if (form.validate()) {
form.save();
return true;
}
return false;
}
}
朋友们能帮助我克服这些障碍吗?
2条答案
按热度按时间mv1qrgav1#
我建议你使用打印和看到你的数据结果从api-call。
iqjalb3h2#
您必须在http调用的头中添加“Content-Type”: