PageView throws '_positions.isNotEmpty': ScrollController not attached to any scroll views

感情迁移 提交于 2021-01-29 19:03:53

问题


I'm trying to change the active page index via a pagecontroller in Flutter, using the Bloc pattern and its throwing "'_positions.isNotEmpty': ScrollController not attached to any scroll views.".

This is my code:

WelcomeWizardScreen:

import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:fluttertest/blocs/wizard/bloc/bloc.dart';
import 'package:fluttertest/screens/wizardsteps/joincongregation.dart';
import 'package:fluttertest/screens/wizardsteps/welcometomapman.dart';

class WelcomeWizardScreen extends StatefulWidget {
  @override
  _WelcomeWizardScreenState createState() => _WelcomeWizardScreenState();
}

class _WelcomeWizardScreenState extends State<WelcomeWizardScreen> {
  final WizardBloc wizardBloc = WizardBloc();

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return BlocProvider(
      builder: (BuildContext context) => WizardBloc(),
      child: PageView(
        children: <Widget>[WelcomeToMapMan(), JoinCongregation()],
        controller: wizardBloc.pageController,

      ),
    );
  }
}

WizardBloc:

import 'package:bloc/bloc.dart';
import 'package:flutter/widgets.dart';
import 'wizard_state.dart';

import 'wizard_event.dart';

class WizardBloc extends Bloc<WizardEvent, WizardState> {
  int activeStep = 0;
  final PageController pageController = PageController(initialPage: 0, keepPage: false, viewportFraction: 0.4);
  @override
  WizardState get initialState => WelcomeToMapManState();


  @override
  Stream<WizardState> mapEventToState(
    WizardEvent event,
  ) async* {

    if (event is ChangePage)
    {

   pageController.jumpToPage(event.pageIndex);


    }
    // TODO: Add Logic
  }


  Stream<WizardState> _mapJoinCongregationToState() async* {


  }
}

One of the screens in the PageView:

class JoinCongregation extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final WizardBloc _wizardBloc = BlocProvider.of<WizardBloc>(context);
    // TODO: implement build
    return Column(
      children: <Widget>[
        Center(
          child: Text("this is step 2"),
        ),
        RaisedButton(
          child: Text("back to step 1"),
          onPressed: () => {_wizardBloc.dispatch(ChangePage(0))},
        )
      ],
    );
  }
}

It seems like the PageViewController isn't "attached" to the PageView when it is called on to change pages, but it initalises correctly (on the correct page index).

How can I solve this? I'm fairly new to flutter.


回答1:


You shouldn't create a PageController in a bloc because a bloc should not be coupled with the UI (theoretically you should be able to reuse your bloc between Flutter and AngularDart). Please refer to https://github.com/felangel/bloc/issues/18 for an example of how you can accomplish this.



来源:https://stackoverflow.com/questions/56623064/pageview-throws-positions-isnotempty-scrollcontroller-not-attached-to-any-sc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!