I want to route to PageNavigation after login, and I need to sure login() returns true before the navigate action , but there's a warning after I update flutter to 3.7.3, thank u so much to help me out though it doesn't affect my app now.
onPressed: () async {
if (_formKey.currentState!.validate()) {
if (await login()) {
Navigator.push(context, MaterialPageRoute(builder: (context) {
return const PageNavigation();
}));
}
}
},
Expected results:
no warnings
Actual results:
Don't use 'BuildContext's across async gaps.
@gkdgo
not a bug, you need to check if context
is mounted
import 'package:flutter/material.dart';
Future<bool> login() => Future.delayed(const Duration(seconds: 1), () => true);
void main() => runApp(const MaterialApp(home: MyApp()));
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(context) => Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () async {
if (await login()) {
if (context.mounted) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const PageNavigation(),
),
);
}
}
},
),
);
}
class PageNavigation extends StatelessWidget {
const PageNavigation({super.key});
@override
Widget build(context) => const Material(
child: Center(
child: Text('you are logged in'),
),
);
}
Owner Name | flutter |
Repo Name | flutter |
Full Name | flutter/flutter |
Language | Dart |
Created Date | 2015-03-06 |
Updated Date | 2023-03-30 |
Star Count | 151602 |
Watcher Count | 3555 |
Fork Count | 25000 |
Issue Count | 11498 |
Issue Title | Created Date | Updated Date |
---|