gpt4 book ai didi

flutter - 不要跨异步间隙使用 BuildContexts flutter

转载 作者:行者123 更新时间:2023-12-05 02:27:49 25 4
gpt4 key购买 nike

我已经创建了一个注册函数,但在 Utils.flushBarErrorMessage("No Internet", context);Do not use BuildContexts across async gaps. 上收到警告 我是 Flutter 的新手,想知道如何使用 asyncawait

Future _registration() async {
String name = _nameController.text.trim();
String email = _emailController.text.trim();
String password = _passwordController.text.trim();
String phone = _phoneController.text.trim();

if (name.isEmpty) {
Utils.flushBarErrorMessage("Type your name", context);
} else if (email.isEmpty) {
Utils.flushBarErrorMessage("Type your email", context);
} else if (!GetUtils.isEmail(email)) {
Utils.flushBarErrorMessage("Type valid email address", context);
} else if (password.isEmpty) {
Utils.flushBarErrorMessage("Type your password", context);
} else if (password.length < 6) {
Utils.flushBarErrorMessage(
"password can't be less than 6 characters", context);
} else if (phone.isEmpty) {
Utils.flushBarErrorMessage("Type your phone", context);
}
else {
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile ||
connectivityResult == ConnectivityResult.wifi) {
ApiCall.signUp(name, email, password, phone).then((value) {
if (value.statusCode == 200) {
if (json.decode(value.body)['success'] != null) {
if (json.decode(value.body)["success"]) {
RegisterResponse registerResponseModel =
RegisterResponse.fromJson(json.decode(value.body));
Navigator.pushNamed(context, VerifyUser.routeName);
Utils.flushBarErrorMessage(
'User Registered Successfully', context);
if (kDebugMode) {
print('User Registered Successfully');
}
} else {
Utils.flushBarErrorMessage(
json.decode(value.body)["en_message"], context);
if (kDebugMode) {
print(json.decode(value.body).toString());
}
}
}
} else {
Utils.flushBarErrorMessage('invalid data', context);
if (kDebugMode) {
print(json.decode(value.body).toString());
}
}
});
} else {
Utils.flushBarErrorMessage("No Internet", context);
}
}
}

调用此 _registration()

ElevatedButton(
onPressed: () {
_registration();
},
child: const Text('SignUp')),

这是我的flushBarErrorMessage

   class Utils {
static void flushBarErrorMessage(String message, BuildContext context) {
showFlushbar(
context: context,
flushbar: Flushbar(
forwardAnimationCurve: Curves.decelerate,
margin: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
padding: const EdgeInsets.all(15),
titleColor: Colors.white,
duration: const Duration(seconds: 3),
borderRadius: BorderRadius.circular(10),
reverseAnimationCurve: Curves.easeInOut,
icon: const Icon(
Icons.error,
size: 28,
color: Colors.white,
),
flushbarPosition: FlushbarPosition.TOP,
positionOffset: 20,
message: message,
backgroundColor: Colors.red,
)..show(context));
}
}

最佳答案

问题是在 await 之后,每次使用 BuildContext 都会显示此警告。出现此警告是因为在处理小部件后可能会在 await 之后使用 BuildContext。这样,上下文将不再存在,应用程序甚至可能因此崩溃。查看the official lint documentation :

Storing BuildContext for later usage can easily lead to difficult-to-diagnose crashes. Asynchronous gaps are implicitly storing BuildContext and are some of the easiest to overlook when writing code.

来自官方文档的简单解决方案是需要检查 State.mounted .在出现警告的每个地方,代码看起来都像这样:

      ...
} else {
if (mounted) Utils.flushBarErrorMessage("No Internet", context);
}
...

关于flutter - 不要跨异步间隙使用 BuildContexts flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72893730/

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