【发布时间】:2021-08-21 08:15:20
【问题描述】:
flutter 新手尝试注册和登录屏幕 ^_^"
-
没有代码错误,但注册无法推送到登录屏幕:在 UI 和控制器 dart 文件中标记没有问题,但测试应用程序返回“”和“未处理的异常:键入 'String ' 不是类型 'FutureOr
的子类型 -
注册和登录中输入的无效/不完整详细信息的错误通知文本不会出现。这应该出现在注册/登录按钮之前。
-
当我尝试运行打印登录页面时,屏幕上没有任何反应。
DART FILE OF SIGNUP BELOW:
class SignUp extends StatefulWidget {
const SignUp ({Key key}) : super(key: key);
@override
_SignUpState createState() => _SignUpState();
}
class _SignUpState extends State<SignUp> {
String username;
String useremail;
String userpw;
String errormessage = '';
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image(
image: AssetImage('logo.png'),
width: 70.0,
),
SizedBox(
height: 10.0,
),
Container(
child: Text('Create Account'),
),
SizedBox(
height: 15.0,
),
Container(
width: 300.0,
child: TextField(
obscureText: false,
onChanged: (value) {
username = value;
}),
),
SizedBox(
height: 8.0,
),
Container(
width: 300.0,
child: TextField(
obscureText: false,
onChanged: (value) {
useremail = value;
}),
),
SizedBox(
height: 8.0,
),
Container(
width: 300.0,
child: TextField(
obscureText: true,
onChanged: (value) {
userpassword = value;
}),
),
SizedBox(
height: 8.0,
),
Container(
child: Text(
errormessage,
style: TextStyle(color: Colors.red[500]),
),
),
SizedBox(height: 12.0),
Container(
margin: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
child: ElevatedButton(
onPressed: () {
Map json = {
"username": username,
"useremail": useremail,
"userpw": userpw,
};
//print(json);
UserSignUp(json).then((result) {
if (result['error'] != null) {
setState(() {
errormessage = result['error'];
});
} else {
setState(() {
errormessage = '';
});
Navigator.push(context,
MaterialPageRoute(builder: (context) => SignInPage()));
}
});
},
style: ElevatedButton.styleFrom(
primary: Color(0xff04A148C),
onPrimary: Colors.blue[900],
padding: EdgeInsets.only(
left: 70.0, top: 10.0, right: 70.0, bottom: 10.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
),
child: Text(
'Sign Up',
style: TextStyle(
color: Colors.white,
fontFamily: 'Roboto',
fontSize: 15.0,
fontWeight: FontWeight.w600,
letterSpacing: 1.0,
),
),
),
),
],
),
),
);
}
}
DART FILE OF CONTROLLER BELOW:
final appkey ="appkey random numbers";
Future<Map> UserSignUp(Map json) async {
json['appkey'] = appkey;
final response = await http.post(
Uri.parse('Sign Up URL'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(json),
);
print(response.body);
if (response.statusCode == 200) {
return jsonDecode(response.body);
} else if (response.statusCode == 401) {
Map error = jsonDecode(response.body);
Map json = {
"error": error['data'][0]['msg'],
};
return json;
} else {
throw Exception('Fail');
}
}
Future<Map> UserSignIn(Map json) async {
json['appkey'] = appkey;
final response = await http.post(
Uri.parse('Sign Up URL'),
headers: <String, String>{
'Content-Type': 'application/json;charset=UTF-8',
},
body: jsonEncode(json),
);
if (response.statusCode == 200) {
return jsonDecode(response.body);
} else if (response.statusCode == 400) {
return jsonDecode(response.body);
} else {
throw Exception('Fail');
}
}
CODE IN THE RUN WINDOW WHEN SIGN-UP IS CLICKED WITH EMPTY TEXT FIELDS ARE BELOW:
Performing hot restart...
Syncing files to device AOSP on IA Emulator...
Restarted application in 679ms.
I/flutter (29028): null
I/chatty (29028): uid=10085(com.example.appname) 1.ui identical 1 line
I/flutter (29028): null
I/flutter (29028): {username: null, useremail: null, userpassword: null}
E/flutter (29028): [ERROR:flutter/lib/ui/ui_dart_state.cc(213)] Unhandled Exception: type 'String' is not a subtype of type 'FutureOr<Map<dynamic, dynamic>>'
E/flutter (29028): #0 registerUser (package:appname/controller.dart:22:5)
E/flutter (29028): <asynchronous suspension>
E/flutter (29028):
p and Sign-in screens not showing:
Made a sign-up and login page with error notification text set to appear if there is incomplete or invalid details.
DART FILE OF SIGNUP BELOW:
class SignUp extends StatefulWidget {
const SignUp ({Key key}) : super(key: key);
@override
_SignUpState createState() => _SignUpState();
}
class _SignUpState extends State<SignUp> {
String username;
String useremail;
String userpw;
String errormessage = '';
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image(
image: AssetImage('logo.png'),
width: 70.0,
),
SizedBox(
height: 10.0,
),
Container(
child: Text('Create Account'),
),
SizedBox(
height: 15.0,
),
Container(
width: 300.0,
child: TextField(
obscureText: false,
onChanged: (value) {
username = value;
}),
),
SizedBox(
height: 8.0,
),
Container(
width: 300.0,
child: TextField(
obscureText: false,
onChanged: (value) {
useremail = value;
}),
),
SizedBox(
height: 8.0,
),
Container(
width: 300.0,
child: TextField(
obscureText: true,
onChanged: (value) {
userpassword = value;
}),
),
SizedBox(
height: 8.0,
),
Container(
child: Text(
errormessage,
style: TextStyle(color: Colors.red[500]),
),
),
SizedBox(height: 12.0),
Container(
margin: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
child: ElevatedButton(
onPressed: () {
Map json = {
"username": username,
"useremail": useremail,
"userpw": userpw,
};
//print(json);
UserSignUp(json).then((result) {
if (result['error'] != null) {
setState(() {
errormessage = result['error'];
});
} else {
setState(() {
errormessage = '';
});
Navigator.push(context,
MaterialPageRoute(builder: (context) => SignInPage()));
}
});
},
style: ElevatedButton.styleFrom(
primary: Color(0xff04A148C),
onPrimary: Colors.blue[900],
padding: EdgeInsets.only(
left: 70.0, top: 10.0, right: 70.0, bottom: 10.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
),
child: Text(
'Sign Up',
style: TextStyle(
color: Colors.white,
fontFamily: 'Roboto',
fontSize: 15.0,
fontWeight: FontWeight.w600,
letterSpacing: 1.0,
),
),
),
),
],
),
),
);
}
}
DART FILE OF CONTROLLER BELOW:
final appkey ="appkey random numbers";
Future<Map> UserSignUp(Map json) async {
json['appkey'] = appkey;
final response = await http.post(
Uri.parse('Sign Up URL'),
headers: <String, String>{
'Content Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(json),
);
print(response.body);
if (response.statusCode == 200) {
return jsonDecode(response.body);
} else if (response.statusCode == 401) {
Map error = jsonDecode(response.body);
Map json = {
"error": error['data'][0]['msg'],
};
return json;
} else {
throw Exception('Fail');
}
}
Future<Map> UserSignIn(Map json) async {
json['appkey'] = appkey;
final response = await http.post(
Uri.parse('Sign Up URL'),
headers: <String, String>{
'Content Type': 'application/json;charset=UTF-8',
},
body: jsonEncode(json),
);
if (response.statusCode == 200) {
return jsonDecode(response.body);
} else if (response.statusCode == 400) {
return jsonDecode(response.body);
} else {
throw Exception('Fail');
}
}
【问题讨论】:
标签: flutter flutter-dependencies flutter-test