如何解决此For循环无法正常工作的错误? for循环在Flutter中仅工作一次
这是一个简单的登录表单。如果用户名和密码匹配,请转到用户 页面,否则转到管理页面。
方法代码:
checkLogin(){
setState(() {
for(var c=0;c < global.user_name_arr.length-1 ; c++){
if(global.user_name_arr[c]==myController.text&&global.user_password_arr[c]==myControllerPwd.text)
Navigator.push(context, MaterialPageRoute(builder: (context)=>user()),);
else
Navigator.push(context, MaterialPageRoute(builder:(context)=>admin()),); }
}); }
RaiseButton代码:
new RaisedButton(
child:new Text("Click"),
onPressed:checkLogin,
)
global.dart
library user_login.globlas;
var user_name_arr=['bhanuka','isuru','sampath'];
var user_password_arr=['1234','123','12'];
答案 0 :(得分:1)
首先,让我们重构代码:)创建一个用户类,如下所示:
class User {
final String name;
final String password;
User(this.name, this.password);
}
接下来,修复您的全局用户集合:
final validUsers = [User('bhanuka', '1234'), User('isuru', '123'), User('sampath', '12')];
现在,使用以下代码执行正确的导航:
checkLogin() {
if (validUsers.indexWhere((user) => user.name == myController.text && user.password == myControllerPwd.text) >= 0) {
Navigator.push(context, MaterialPageRoute(builder: (context)=>user()),);
} else {
Navigator.push(context, MaterialPageRoute(builder:(context)=>admin()),);
}
}
有更好的方法进行这种比较,但我想对于您的用例来说已经足够了。
答案 1 :(得分:0)
在这里,您是否要使用其他条件以使条件正确或错误,其中一部分将被执行。
import 'package:flutter/material.dart';
void main() => runApp(new MaterialApp(
title: 'Forms in Flutter',
home: new LoginPage(),
));
class LoginPage extends StatefulWidget {
@override
State<StatefulWidget> createState() => new _LoginPageState();
}
class _LoginData {
String email = '';
String password = '';
}
class _LoginPageState extends State<LoginPage> {
final GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
_LoginData _data = new _LoginData();
var user_name_arr = ['bhanuka', 'isuru', 'sampath'];
var user_password_arr = ['1234', '123', '12'];
var p;
void submit() {
if (this._formKey.currentState.validate()) {
_formKey.currentState.save(); // Save our form now.
if (user_name_arr.contains(_data.email)) {
p = user_name_arr.indexOf(_data.email);
if (user_password_arr.elementAt(p) == _data.password) {
Navigator.push(context, MaterialPageRoute(builder: (context)=>user()),);
} else {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => admin()),
);
}
} else {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => admin()),
);
}
}
}
@override
Widget build(BuildContext context) {
final Size screenSize = MediaQuery.of(context).size;
return new Scaffold(
appBar: new AppBar(
title: new Text('Login'),
),
body: new Container(
padding: new EdgeInsets.all(20.0),
child: new Form(
key: this._formKey,
child: new ListView(
children: <Widget>[
new TextFormField(
keyboardType: TextInputType
.emailAddress, // Use email input type for emails.
decoration: new InputDecoration(
hintText: 'you@example.com',
labelText: 'E-mail Address'),
onSaved: (String value) {
this._data.email = value;
}),
new TextFormField(
obscureText: true, // Use secure text for passwords.
decoration: new InputDecoration(
hintText: 'Password', labelText: 'Enter your password'),
onSaved: (String value) {
this._data.password = value;
}),
new Container(
width: screenSize.width,
child: new RaisedButton(
child: new Text(
'Login',
style: new TextStyle(color: Colors.white),
),
onPressed: this.submit,
color: Colors.blue,
),
margin: new EdgeInsets.only(top: 20.0),
)
],
),
)),
);
}
}
class user extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Center(child: new Text("user")),
),
);
}
}
class admin extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Center(child: new Text("admin")),
),
);
}
}