[英]Showing warning: Do not use BuildContexts across async gaps
if (_formKey.currentState!.validate()) {
try {
final newUser =
await _auth.createUserWithEmailAndPassword(
email: email.text, password: password.text);
if (newUser != null) {
// Navigator.push(
// context,
// MaterialPageRoute(
// builder: (context) => DashboardScreen(),
// ));
Navigator.pushNamed(context, 'dashboard');
}
setState(() {});
} catch (e) {
print(e);
}
}
},
此警告顯示在 Navigator.pushNamed(context,'dashboard'); 試圖導航到儀表板屏幕。
1.你必須延遲其他進程才能完成
Future.delayed(Duration(milliseconds: 200)).then((value) {
Navigator.pushNamed(context, 'dashboard')
});
2.添加if (;mounted) return;
在Navigator.pushNamed(context, 'dashboard')
之前
3.請在navigator flutter之前放置await因為你使用了異步方法調用所以你必須等到過程完成才能導航到你的頁面
await Navigator.pushNamed(context, 'dashboard');
4.此外,您可以將navigator
器存儲到一個var
中,然后使用它。
final nav = Navigator.of(context);
nav.pushNamed('dashboard');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.