使用 BLoC : The context used to push routes from the Navigator must be a descendant of a Navigator widget 的 Flutter 导航

标签 flutter navigation bloc

我正在使用 BLoC,我需要从 PageOne 导航到 PageTwo 并能够使用后退按钮返回,我不知道这是否是处理此问题的正确方法。 当函数 _navigateToPage2 被调用时,我也遇到了错误。

The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.

class SimpleBlocDelegate extends BlocDelegate {

  @override
  void onTransition(Transition transition) {
     print(transition);
  }

  @override
  void onError(Object error, StackTrace stacktrace) {
      print(error);
   }
}



void main() {
  BlocSupervisor().delegate = SimpleBlocDelegate();
  runApp(MyApp(userRepository: UserRepository(GuriApi())));
}

class MyApp extends StatefulWidget {
  final UserRepository userRepository;

  MyApp({Key key, @required this.userRepository}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  AuthenticationBloc _authenticationBloc;
  UserRepository get _userRepository => widget.userRepository;

  @override
  void initState() {
    _authenticationBloc = AuthenticationBloc(userRepository: _userRepository);
    _authenticationBloc.dispatch(AppStarted());
    super.initState();
  }

  @override
  void dispose() {
    _authenticationBloc.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return BlocProvider<AuthenticationBloc>(
      bloc: _authenticationBloc,
      child: MaterialApp(
        theme: new ThemeData(
            fontFamily: 'Monserrat',
            primaryColor: Colors.lightBlue[50],
            accentColor: Colors.white),
        home: BlocBuilder<AuthenticationEvent, AuthenticationState>(
          bloc: _authenticationBloc,
          builder: (BuildContext context, AuthenticationState state) {
            if (state is AuthenticationUninitialized) {
              return SplashPage();
            }
            if (state is AuthenticationAuthenticated) {
              return Home(userRepository: _userRepository);
            }
            if (state is AuthenticationUnauthenticated) {
              return LoginPage(userRepository: _userRepository);
            }
            if (state is AuthenticationLoading) {
              return LoadingIndicator();
            }
            if (state is PageOneSelected) {
              return PageOne();
            }
            if (state is PageTwoSelected) {
              _navigateToPage2();
            }
          },
        ),
      ),
    );
  }

  _navigateToPage2() {
    Navigator.of(context).push<bool>(
        MaterialPageRoute(
            builder: (BuildContext context) =>
                PageTwo(userRepository: _userRepository)));
  }
}

最佳答案

我认为通过订阅 initState 中的 bloc 状态变化来处理导航会更好:

@override
void initState() {
    super.initState();

    bloc.state.listen((state) {
        if (state is PageOneSelected) {
            _navigateToPage2();
        } else if (state is PageTwoSelected) {
            _navigateToPage2();
        }
    });
}

在构建方法中显示一些其他小部件。您的代码中发生错误,因为 blocBuilder 必须返回 Widget 但在 PageTwoSelected 状态的情况下,您什么也不返回。

关于使用 BLoC : The context used to push routes from the Navigator must be a descendant of a Navigator widget 的 Flutter 导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54752882/

相关文章:

google-maps - 如何修改谷歌地图导航器控件样式

javascript - Backbone.js 和 router.navigate

android - 放置图标的底部应用栏问题

flutter - Dart Provider:在直子上不可用

flutter - 带有 StreamBuilder 的 TextField 的初始值

flutter - 如何从ListView获取值列表

flutter - 防止 BoxShadow 被父级裁剪

Flutter Bloc 显示 : "Unexpected null value"

flutter - 在 flutter 中添加 video_trimmer 后 better_player 停止工作

dart - 无法使用 setState