flutter - 应用程序无法正常工作,因为&/ 未处理的异常:“String”类型不是“FutureOr”类型的子类型
问题描述
颤振新手尝试做一个注册和登录屏幕^_^"
没有代码错误,但注册无法推送到登录屏幕:在 UI 和控制器 dart 文件中标记没有问题,但测试应用程序返回“<异步挂起>”和“未处理的异常:类型 'String' 不是'FutureOr<Map<dynamic, dynamic>>' 类型的子类型
注册和登录中输入的无效/不完整详细信息的错误通知文本不会出现。这应该出现在注册/登录按钮之前。
当我尝试运行打印登录页面时,屏幕上没有任何反应。
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');
}
}
解决方案
我明白了,你需要使用await
withjsonDecode
函数,因为它是一个未来。前任 :
Map res = await jsonDecode(response.body);
return res;
代替:
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');
}
}
和 :
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) {
Map data = await jsonDecode(response.body);
return data;
} else if (response.statusCode == 401) {
Map error = await 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) {
Map data = await jsonDecode(response.body);
return data;
} else if (response.statusCode == 400) {
Map data = await jsonDecode(response.body);
return data;
} else {
throw Exception('Fail');
}
}
推荐阅读
- swift - 使用 Firebase 为 ios 设置 Stripe 无服务器
- xcode - Realm 5.0.1 文件在 Xcode 11.4 中显示为空
- python - 查看本地目录是否存在
- r - 随机抽样直到达到总和
- reactjs - 使用带有 Material-UI 输入的 react-text-mask 时呈现“轮廓”输入
- reactjs - 我有 nginx 运行,但它不服务于我的反应构建
- java - 是否有返回 Android 应用程序身份验证级别的 API/库
- python - ufunc 'subtract' 不包含具有签名匹配类型的循环(dtype('
dtype(' 我也尝试减去两个矩阵,首先我使用 np.substract 然后我只是使用'-'。矩阵减法的代码工作得很好,但是当我向我的函数输入相同的代码时,python 给出了这个错误
Traceback (most recent call last): File "
- c# - 使用属性的WEB API验证类
- python - 无法在小部件上添加 id - Kiy