gpt4 book ai didi

flutter - 当使用 setState() 出现 setState() 回调参数时返回一个 Future

转载 作者:行者123 更新时间:2023-12-03 13:29:50 27 4
gpt4 key购买 nike

当我使用 setState () 文本出现在调试控制台中..

setState() callback argument returned a Future. The setState() method on _LoginActivityState#9cd91 was called with a closure or method that returned a Future. Maybe it is marked as "async". Instead of performing asynchronous work inside a call to setState(), first execute the work (without updating the widget state), and then synchronously update the state inside a call to setState().


   Future<void> login() async {
final formState = formKey.currentState;
if (formState.validate()) {
formState.save();
try {
final response = await UserController.login({
"username": username,
"password": password,
});

if (response != null && response["success"]) {
setState(() async {
accessToken = response['token'];
//print(token);
if (accessToken != null) {
await Http.setAccessToken("Bearer $accessToken");
}
print('$accessToken');
final getMe = await AiframeworkController.getProfile();
print('data: $getMe');
if (getMe != null && getMe['is_verified'] == true) {
return Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => MainActivity()));
} else {
return Center(
child: CircularProgressIndicator(),
);
}
});
}
} catch (e) {
print(e.message);
}
}

}

最佳答案

setState() 应该仅用于设置有状态小部件的新状态。您不应该在其中执行异步操作,也不应该从中返回任何内容。我假设“accessToken”是您要更改其状态的字段,因此最好在 setState() 之外执行所有其他操作,然后离开 'accessToken = response['token'];'里面。

关于flutter - 当使用 setState() 出现 setState() 回调参数时返回一个 Future,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56947080/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com