我正在为Flutter应用开发身份验证。请帮助我解决问题。我无法识别出返回此类错误的问题,指出未处理的异常:类型“列表”不是类型“字符串”的子类型。
以下是登录屏幕的代码:
from rest_framework.views import APIView
class CreateOrder(APIView):
def post(self, request):
....
上述代码部分以执行登录:
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class WelcomeBackPage extends StatefulWidget {
@override
WelcomeBackPageState createState() => WelcomeBackPageState();
}
class WelcomeBackPageState extends State<WelcomeBackPage> {
final _scaffoldKey = GlobalKey<ScaffoldState>();
final _formKey = GlobalKey<FormState>();
bool _isSubmitting, _obscureText = true;
String _email, _password;
Widget _showHello() {
return Text(
'Hello',
style: GoogleFonts.montserrat(
fontSize: 70.0,
fontWeight: FontWeight.w800,
textStyle: TextStyle(color: Colors.black)),
);
}
Widget _showThere() {
return Container(
padding: EdgeInsets.only(left: 5.0),
child: Text(
'There',
style: GoogleFonts.montserrat(
fontSize: 70.0,
fontWeight: FontWeight.w800,
height: 0.7,
textStyle: TextStyle(color: Colors.black)),
));
}
Widget _showDot() {
return Text(
'.',
style: GoogleFonts.montserrat(
fontSize: 70.0,
fontWeight: FontWeight.bold,
height: 0.7,
textStyle: TextStyle(color: Colors.green)),
);
}
Widget _showEmailInput() {
return Padding(
padding: EdgeInsets.only(top: 30.0, left: 5.0),
child: TextFormField(
onSaved: (val) => _email = val,
decoration: InputDecoration(
labelText: 'EMAIL',
labelStyle: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.grey[300]),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.green)),
),
keyboardType: TextInputType.emailAddress,
validator: (val) => !val.contains('@') ? 'Invalid Email' : null,
));
}
Widget _showPasswordInput() {
return Padding(
padding: EdgeInsets.only(top: 10.0, left: 5.0),
child: TextFormField(
onSaved: (val) => _password = val,
decoration: InputDecoration(
suffixIcon: GestureDetector(
onTap: () {
setState(() => _obscureText = !_obscureText);
},
child: Icon(
_obscureText ? Icons.visibility : Icons.visibility_off)),
labelText: 'PASSWORD',
labelStyle: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.grey[300]),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.green)),
),
obscureText: _obscureText,
validator: (val) => val.length < 6 ? 'Invalid Password' : null,
));
}
Widget _showForgotPassword() {
return Container(
alignment: Alignment(1.2, 0.0),
child: Padding(
padding: EdgeInsets.only(top: 0.0),
child: Column(
children: <Widget>[
FlatButton(
onPressed: () {},
child: Text(
'Forgot Password',
style: GoogleFonts.montserrat(
color: Colors.green,
fontSize: 16.0,
fontWeight: FontWeight.bold,
decoration: TextDecoration.underline),
),
),
],
),
));
}
Widget _showLogin() {
return Padding(
padding: EdgeInsets.only(top: 20.0),
child: ButtonTheme(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(100)),
child: Center(
child: _isSubmitting == true
? CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation(Colors.green),
)
: RaisedButton(
onPressed: _login,
textColor: Colors.white,
color: Colors.green,
padding: EdgeInsets.all(5.0),
child: Container(
height: 50,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(100),
color: Colors.green),
padding: const EdgeInsets.all(15.0),
child: Text(
'LOGIN',
textAlign: TextAlign.center,
style: GoogleFonts.montserrat(
letterSpacing: 2.0,
fontSize: 18.0,
fontWeight: FontWeight.w600,
),
),
),
))));
}
void _login() {
final form = _formKey.currentState;
if (form.validate()) {
form.save();
_registerUser();
}
}
void _registerUser() async {
setState(() => _isSubmitting = true);
http.Response response = await http.post(
'http://192.168.43.9:1337/auth/local',
body: {"identifier": _email, "password": _password});
final responseData = json.decode(response.body);
if (response.statusCode == 200) {
setState(() => _isSubmitting = false);
_showSuccessSnack();
_redirectUser();
print(responseData);
} else {
setState(() => _isSubmitting = false);
final String errorMsg = responseData['message'];
_showErrorSnack(errorMsg);
}
}
void _showSuccessSnack() {
final snackbar = SnackBar(
content: Text(' User successfully logged in ',
style:
GoogleFonts.montserrat(color: Colors.green, fontSize: 16.0)));
_scaffoldKey.currentState.showSnackBar(snackbar);
_formKey.currentState.reset();
}
void _showErrorSnack(String errorMsg) {
final snackbar = SnackBar(
content: Text(errorMsg,
style: GoogleFonts.montserrat(color: Colors.red, fontSize: 16.0)));
_scaffoldKey.currentState.showSnackBar(snackbar);
throw Exception('Error logging in: $errorMsg');
}
void _redirectUser() {
Future.delayed(Duration(seconds: 2), () {
Navigator.pushReplacementNamed(context, '/intro');
});
}
Widget _showGoogle() {
return Padding(
padding: EdgeInsets.only(top: 20.0),
child: OutlineButton(
shape: StadiumBorder(),
textColor: Colors.black,
borderSide: BorderSide(
color: Colors.black, style: BorderStyle.solid, width: 2.5),
onPressed: () {},
padding: EdgeInsets.all(5.0),
child: Container(
height: 50,
width: MediaQuery.of(context).size.width,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Center(
child: ImageIcon(AssetImage('assets/facebook.png')),
),
SizedBox(width: 10.0),
Center(
child: Text(
'Log in with Google',
textAlign: TextAlign.center,
style: GoogleFonts.montserrat(
fontSize: 18.0,
fontWeight: FontWeight.w600,
),
),
)
],
),
),
));
}
Widget _showNewRegister() {
return Padding(
padding: EdgeInsets.only(top: 35.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'New to Oristore ?',
style: GoogleFonts.montserrat(
fontSize: 16.0,
fontWeight: FontWeight.w600,
),
),
SizedBox(width: 5.0),
InkWell(
onTap: () => Navigator.pushReplacementNamed(context, '/register'),
child: Text(
'Register',
style: GoogleFonts.montserrat(
fontSize: 17.0,
color: Colors.green,
fontWeight: FontWeight.w600,
decoration: TextDecoration.underline,
),
),
)
],
));
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
resizeToAvoidBottomPadding: false,
resizeToAvoidBottomInset: true,
body: SingleChildScrollView(
child: Form(
key: _formKey,
child: Stack(children: <Widget>[
Container(
padding: EdgeInsets.symmetric(horizontal: 20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(
height: 60.0,
),
Padding(
padding: EdgeInsets.all(10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
_showHello(),
new Row(children: <Widget>[
_showThere(),
_showDot(),
]),
_showEmailInput(),
_showPasswordInput(),
_showForgotPassword(),
_showLogin(),
_showGoogle(),
_showNewRegister()
],
))
])),
//_showOTPAction(),
]))));
}
}
错误
void _login() {
final form = _formKey.currentState;
if (form.validate()) {
form.save();
_registerUser();
}
}
void _registerUser() async {
setState(() => _isSubmitting = true);
http.Response response = await http.post(
'http://192.168.43.9:1337/auth/local',
body: {"identifier": _email, "password": _password});
final responseData = json.decode(response.body);
if (response.statusCode == 200) {
setState(() => _isSubmitting = false);
_showSuccessSnack();
_redirectUser();
print(responseData);
} else {
setState(() => _isSubmitting = false);
final String errorMsg = responseData['message'];
_showErrorSnack(errorMsg);
}
}
void _showSuccessSnack() {
final snackbar = SnackBar(
content: Text(' User successfully logged in ',
style:
GoogleFonts.montserrat(color: Colors.green, fontSize: 16.0)));
_scaffoldKey.currentState.showSnackBar(snackbar);
_formKey.currentState.reset();
}
void _showErrorSnack(String errorMsg) {
final snackbar = SnackBar(
content: Text(errorMsg,
style: GoogleFonts.montserrat(color: Colors.red, fontSize: 16.0)));
_scaffoldKey.currentState.showSnackBar(snackbar);
throw Exception('Error logging in: $errorMsg');
}
void _redirectUser() {
Future.delayed(Duration(seconds: 2), () {
Navigator.pushReplacementNamed(context, '/intro');
});
}
答案 0 :(得分:0)
尝试使用此val?.length
以使val
可为空。
答案 1 :(得分:0)
onSaved:(val)=> _email = val, onSaved:(val)=> _password = val,
应该添加到textformfield输入中,以使val不会返回为空。
使用整个flutter应用程序重新启动服务器api解决了该问题。 现在,用户可以成功登录。