【发布时间】:2021-07-10 17:03:59
【问题描述】:
我对 Flutter 有点陌生。我的请求有什么问题?
我的某个朋友配置了一个 SAP Web 服务,而您将在代码中看到的 _kAuth 是这样定义的所需令牌:
static const _kAuth = "Basic $tokenhiddenforprivacy=";
我得到了什么:
- 错误:一般错误(下面的屏幕截图)。这只是一个希望错误的令牌的问题吗?我的代码是否存在问题,因为它处理 Null-Safe 错误?
- 错误:在第一个错误之后,我得到了这个操作系统错误:没有与主机名关联的地址,errno = 7
主要是:
class CredentialsRecoveryPage extends StatefulWidget {
const CredentialsRecoveryPage({Key? key}) : super(key: key);
static const routeName = '/CredentialsRecoveryPage';
@override
_CredentialsRecoveryPageState createState() =>
_CredentialsRecoveryPageState();
}
class _CredentialsRecoveryPageState extends State<CredentialsRecoveryPage> {
final TextEditingController _emailController = TextEditingController();
final _formKey = GlobalKey<FormState>();
bool _alreadyTapped = true;
@override
void dispose() {
_emailController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container(
child: Scaffold(
appBar: AppBar(
elevation: 0,
),
resizeToAvoidBottomInset: true,
body: Form(
key: _formKey,
child: Padding(
padding: const EdgeInsets.fromLTRB(20, 0, 20, 30),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("Seems like you forgot something...",
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.w600,
)),
SizedBox(
height: 10,
),
Text("We need your mail",
style: TextStyle(
fontSize: 18,
// fontWeight: FontWeight.w500,
)),
SizedBox(
height: 10,
),
TextFormField(
controller: _emailController,
keyboardType: TextInputType.emailAddress,
validator: (value) => _validateMail(value!),
style: TextStyle(color: Theme.of(context).accentColor),
decoration: mailInputDecoration(context),
onSaved: (value) => _emailController.text = value!),
SizedBox(
height: 15,
),
Text("The mail will be delivered within 10 minutes",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 10,
// fontWeight: FontWeight.w500,
)),
Visibility(
visible: _alreadyTapped,
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
InkWell(
onTap: () => _sendCredentialRequest(context),
child: Icon(LineIcons.paperPlane,
color: Theme.of(context).accentColor),
),
]),
),
]),
),
),
),
);
}
String? _validateMail(String mail) {
if (_emailController.text.isEmpty) {
return 'Mail field cant\'t be empty';
} else if (!_emailController.text.toUpperCase().contains("@")) {
return 'Mail is not valid';
} else {
return null;
}
}
Future<dynamic> _sendCredentialRequest(BuildContext ctx) async {
if (_formKey.currentState!.validate()) {
startLoadingSpinner(ctx);
try {
var resp =
await NetworkManager().getForCredentials(_emailController.text);
if (resp.statusCode >= 200 && resp.statusCode <= 300) {
var message = SapReturnMessage.fromJson(jsonDecode(resp.body));
Navigator.pop(ctx);
setState(() {
_alreadyTapped = false;
});
return message.returnSnackByMessage(ctx);
} else {
Navigator.pop(ctx);
SnackBarMessage.genericError(ctx, "Something went wrong!");
}
} catch (onError) {
Navigator.pop(ctx);
SnackBarMessage.genericError(ctx, onError.toString());
}
}
}
}
然后在我的 networkManager.dart 我有这个 getForCredentials 类:
Future<http.Response> getForCredentials(String mail) async {
var url =
"http://mylinkhiddenforprivacy";
return await http
.get(Uri.parse(url), headers: {
"Authorization": _kAuth,
"Accept": "application/json",
})
.then((response) {
return response;
})
.timeout(Duration(seconds: 20))
.catchError((onError) {
print(onError.toString());
});
}
这是输出: Screenshot
【问题讨论】:
-
我猜这是因为你没有在 getForCredentials() 函数的 catchError 中返回任何内容
-
我添加了“return http.Response('', 500);”正如您所建议的那样,我正确地得到了“出了点问题!” “credentialRecoveryPage.dart”代码中的 Snackbar 消息,所以我们可以假设服务器连接的问题可能只是错误的令牌?
-
您可以为 catchError 提供第二个参数作为 'stacktrace'。将两个 args 打印到控制台以查看实际错误。
-
如果您不确定令牌测试是否使用 Postman