我的应用程序中包含以下主页:
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final CategoriesBloc _categoriesBloc = BlocProvider.of<CategoriesBloc>(context);
return Scaffold(
appBar: AppBar(
title: Text('E-Commerce'),
centerTitle: true,
actions: <Widget>[CartButton()],
),
body: StreamBuilder(
stream: _categoriesBloc.outCategories,
builder: (BuildContext context, AsyncSnapshot<List<Category>> categories) {
if (categories.hasData) {
return ListView.builder(
itemCount: categories.data.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(categories.data[index].name,
style: TextStyle(fontSize: 24.0)),
onTap: () => Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) => BlocProvider<ProductsBloc>(
child: SelectedCategoryPage(),
bloc: ProductsBloc(categories.data[index]),
))));
},
);
}
return SizedBox();
},
));
}}
其中使用了类别bloc:
class CategoriesBloc implements BlocBase {
List<Category> _categories;
StreamController<List<Category>> _categoriesController =
StreamController<List<Category>>();
Sink<List<Category>> get _inCategories => _categoriesController.sink;
Stream<List<Category>> get outCategories => _categoriesController.stream;
CategoriesBloc() {
getCategories();
}
void getCategories() {
DbApi dbApi = DbApi();
_categories = dbApi.getCategories();
_inCategories.add(_categories);
}
@override
void dispose() {
_categoriesController.close();
}
}
我目前正在尝试一个Udemy课程,试图学习Flutter,我的老师没有回答的问题是,您看到以下一行:
stream: _categoriesBloc.outCategories,
这将返回所有类别,没有问题,但是它如何访问数据?因为从类别组中,我只用数据填充_inCategories?我已经遍历了所有代码,但是找不到任何地方填充了Category?
希望对它的实际工作方式进行澄清。