【发布时间】:2021-09-09 06:57:16
【问题描述】:
在迁移到零安全之前,这很有效。这是一种用户登录验证方式。
Future<bool> loginValidate() async {
final String dir = await getDocDir(); //getting documents directory
try {
await File('$dir/$_userLoginString.json')
.readAsString()
.then((String contents) {
final json = jsonDecode(contents) as Map<String, dynamic>;
final user = PersonLoginJson.fromJson(json);
if (_userPasswordString != user.password) {
//invalid password
return Future<bool>.value(false);
}
});
} on FileSystemException {
//invalid username
return Future<bool>.value(false);
} catch (e) {
return Future<bool>.value(false);
}
//success
return Future<bool>.value(true);
}
此error 在构建应用程序时出现。
我认为这与 .then() 方法中的匿名函数参数有关。
【问题讨论】:
-
您的回调函数忽略了沿所有代码路径返回值。因此,如果不满足
if条件,它会隐式返回null,并且null返回值是意外的。修复您的代码以沿所有代码路径返回值。另外await和then混用也不一致;只需使用await,这会使您的问题更加明显。
标签: flutter dart typeerror future dart-null-safety