我开始使用 Flutter 框架进行编程,并立即注意到出现了一个奇怪的问题(有关更多信息,请参见下文)。
问题发生在以下情况:返回MainScreen
并尝试PageView
通过按下按钮从 中的屏幕切换来更改索引后PageView
,我收到此错误:
[ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: 'package:flutter/src/widgets/scroll_controller.dart': Failed assertion: line 110 pos 12: '_positions.isNotEmpty': ScrollController not attached to any scroll views.
这是我的主屏幕:
class MainScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() => MainScreenState();
}
class MainScreenState extends State<MainScreen> {
PageController _controller;
void _onLoad() {
_controller = PageController();
ShareBloc.getInstance().pageViewIndexStream.listen((index) {
_controller.animateToPage(index,
duration: Duration(milliseconds: 200), curve: Curves.easeInOut);
});
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
_onLoad();
return PageView(
controller: _controller,
physics: NeverScrollableScrollPhysics(),
children: [
FirstScreen(),
SecondScreen(),
],
);
}
}
这是我的RxDart
Sharable
文件:
import 'package:rxdart/rxdart.dart';
class ShareBloc {
static ShareBloc _instance;
BehaviorSubject<dynamic> _subjectCounter;
static ShareBloc getInstance() {
if (_instance == null) _instance = ShareBloc();
return _instance;
}
ShareBloc() {
_subjectCounter = new BehaviorSubject<dynamic>();
}
Stream<dynamic> get getStream => pageViewIndexStream.stream;
void onShare(dynamic data) {
_subjectCounter.sink.add(data);
}
void dispose() {
if (_subjectCounter != null) {
_subjectCounter.close();
_instance = null;
}
}
}
有人可以告诉我我在这里做错了什么吗?