如何在没有上下文的情况下导航?
How to Navigate without context in flutter?
我最终使用了静态函数,但我需要进行导航,它给了我一个错误,没有找到上下文的 getter,所以我寻找解决方案并找到了 GET 包,但是当我试图使用它它给了我另一个错误:
E/flutter ( 6078): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)]
Unhandled Exception: NoSuchMethodError: The method 'push' was called on null.
我的代码:
void main() {
runApp(MyApp());
_MyAppState.autologin();
}
class _MyAppState extends State<MyApp> {
static autologin() async {
var userType;
var store = Firestore.instance;
var auth = FirebaseAuth.instance;
final FirebaseUser user = await auth.currentUser();
store.collection('Users').document(user.uid).get().then((value) {
userType = (value.data)['userType'];
if (userType == 'Student') {
Get.to(StudentsPage());
} else if (userType == 'Teacher') {
} else if (userType == 'Admin') {}
});
}
创建导航键
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
将其分配给 MaterialApp
MaterialApp(
home: Home(),
navigatorKey: navigatorKey
),
然后通过下面的navigatorKey推送你的路线
navigatorKey.currentState.push(MaterialPageRoute(
builder: (context) => AnotherPage(),
));
或
navigatorKey.currentState.pushNamed(routeName);
如果您想使用 globalKey 在没有上下文的情况下导航或显示对话框,尤其是在 Bloc 中,或者当您的逻辑与 UI 部分分开时,此解决方案是通用的。
首先安装这个包:
注意:我使用的是空安全版本
get_it: ^7.2.0
然后为您的服务定位器创建一个单独的文件:
service_location.dart
import 'package:get_it/get_it.dart';
GetIt locator = GetIt.instance;
class NavigationService {
final GlobalKey<NavigatorState> navigatorKey =
new GlobalKey<NavigatorState>();
Future<dynamic> navigateTo(String routeName) {
return navigatorKey.currentState!.pushNamed(routeName);
}
void setupLocator() {
locator.registerLazySingleton(() => NavigationService());
}
void showMyDialog() {
showDialog(
context: navigatorKey.currentContext!,
builder: (context) => Center(
child: Material(
color: Colors.transparent,
child: Text('Hello'),
),
));
}
}
于 main.dart:
void main() {
WidgetsFlutterBinding.ensureInitialized();
NavigationService().setupLocator();
runApp(MyApp());
}
// add navigatorKey for MaterialApp
MaterialApp(
navigatorKey: locator<NavigationService>().navigatorKey,
),
在您的业务逻辑文件中 bloc.dart
在 bloc class 或任何 class 你想在里面使用导航的地方定义它
然后开始在里面任意函数里面导航。
class Cubit extends Cubit<CubitState> {
final NavigationService _navigationService = locator<NavigationService>();
void sampleFunction(){
_navigationService.navigateTo('/home_screen'); // to navigate
_navigationService.showMyDialog(); // to show dialog
}
}
不是:我正在使用 generateRoute 进行路由。
我最终使用了静态函数,但我需要进行导航,它给了我一个错误,没有找到上下文的 getter,所以我寻找解决方案并找到了 GET 包,但是当我试图使用它它给了我另一个错误:
E/flutter ( 6078): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)]
Unhandled Exception: NoSuchMethodError: The method 'push' was called on null.
我的代码:
void main() {
runApp(MyApp());
_MyAppState.autologin();
}
class _MyAppState extends State<MyApp> {
static autologin() async {
var userType;
var store = Firestore.instance;
var auth = FirebaseAuth.instance;
final FirebaseUser user = await auth.currentUser();
store.collection('Users').document(user.uid).get().then((value) {
userType = (value.data)['userType'];
if (userType == 'Student') {
Get.to(StudentsPage());
} else if (userType == 'Teacher') {
} else if (userType == 'Admin') {}
});
}
创建导航键
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
将其分配给 MaterialApp
MaterialApp(
home: Home(),
navigatorKey: navigatorKey
),
然后通过下面的navigatorKey推送你的路线
navigatorKey.currentState.push(MaterialPageRoute(
builder: (context) => AnotherPage(),
));
或
navigatorKey.currentState.pushNamed(routeName);
如果您想使用 globalKey 在没有上下文的情况下导航或显示对话框,尤其是在 Bloc 中,或者当您的逻辑与 UI 部分分开时,此解决方案是通用的。
首先安装这个包:
注意:我使用的是空安全版本
get_it: ^7.2.0
然后为您的服务定位器创建一个单独的文件:
service_location.dart
import 'package:get_it/get_it.dart';
GetIt locator = GetIt.instance;
class NavigationService {
final GlobalKey<NavigatorState> navigatorKey =
new GlobalKey<NavigatorState>();
Future<dynamic> navigateTo(String routeName) {
return navigatorKey.currentState!.pushNamed(routeName);
}
void setupLocator() {
locator.registerLazySingleton(() => NavigationService());
}
void showMyDialog() {
showDialog(
context: navigatorKey.currentContext!,
builder: (context) => Center(
child: Material(
color: Colors.transparent,
child: Text('Hello'),
),
));
}
}
于 main.dart:
void main() {
WidgetsFlutterBinding.ensureInitialized();
NavigationService().setupLocator();
runApp(MyApp());
}
// add navigatorKey for MaterialApp
MaterialApp(
navigatorKey: locator<NavigationService>().navigatorKey,
),
在您的业务逻辑文件中 bloc.dart 在 bloc class 或任何 class 你想在里面使用导航的地方定义它 然后开始在里面任意函数里面导航。
class Cubit extends Cubit<CubitState> {
final NavigationService _navigationService = locator<NavigationService>();
void sampleFunction(){
_navigationService.navigateTo('/home_screen'); // to navigate
_navigationService.showMyDialog(); // to show dialog
}
}
不是:我正在使用 generateRoute 进行路由。