我有一个有 2 页的应用程序,当主页按钮上的选项卡导航到设置页面时。
IconButton(
onPressed:(){
Navigator.push(
context,
MaterialPageRoute( builder: (context) => Settings())
);
},
),
但是在设置页面上,来自我的块的流数据都是 NULL。我不知道为什么,在主页上都很好。
我在设置页面的 Bloc 中添加了新数据作为测试,它显示得很好,所以 Bloc 实现工作正常,只是没有在 bloc 构造函数中获取启动的数据。
我做错了什么?
Bloc 数据在 bloc 构造函数中启动,这是我的 Bloc 代码
class StateBloc implements BlocBase {
var selectedCurrencies;
StreamController<List> _selectedCurrencies = StreamController<List>.broadcast();
Stream<List> get getSelectedCurrencies => _selectedCurrencies.stream;
StreamSink get modifySelectedCurrencies => _selectedCurrencies.sink;
StateBloc() {
selectedCurrencies = ['cny','aud','usd'];
modifySelectedCurrencies.add(selectedCurrencies);
}
}
这是 bloc 如何提供给 main.dart
Future<void> main() async{
return runApp(
BlocProvider<StateBloc>(
child: MyApp(),
bloc: StateBloc(),
),
);
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Exchange',
debugShowCheckedModeBanner: false,
theme: ThemeData(fontFamily: 'Poppins'),
home: MyHomePage(),
);
}
}
这是我的设置页面
class Settings extends StatelessWidget {
@override
Widget build(BuildContext context) {
final StateBloc appBloc = BlocProvider.of<StateBloc>(context);
return MaterialApp(
home: Scaffold(
body: ListView(
children: <Widget>[
StreamBuilder(
stream: appBloc.getSelectedCurrencies,
builder: (context, selectedCurrenciesSnap) {
print('alllll --> ${allCurrenciesSnap.data}');
}
)
]
)
)
)
}
}