gpt4 book ai didi

flutter - 为什么在Flutter Sink中添加数据不起作用?

转载 作者:行者123 更新时间:2023-12-02 04:21:32 25 4
gpt4 key购买 nike

目的很简单。获取数据后,可以通过特定的字符串集进行过滤。因此,我最初使用“全部”进行过滤,这意味着显示所有数据,并在单击任何选择的筹码时基于该特定字符串进行过滤。一切正常,除了从api调用加载数据后不显示所有数据。即使我再次热装,它也会显示完整列表数据。因此,基本上在Sink中添加字符串数据是行不通的。我认为我犯了一些愚蠢的错误,但无法弄清楚。需要建议。

BLOC类(class)

final Application _application;

ProductListScreenBloc(this._application);
int totalPages = 1;


final _productList = BehaviorSubject<List<Product>>();
Observable<List<Product>> _filteredProductList = Observable.empty();
final _filterName = BehaviorSubject<String>();


Stream<List<Product>> get productList => _productList.stream;
Stream<List<Product>> get filteredProductList => _filteredProductList;
Sink<String> get filterName => _filterName;


void loadBrandWiseProductList(
String categorySlug, String brandSlug, int pageNo) {

if (totalPages >= pageNo) { //for pagination

StreamSubscription subscription = _application.productListRepository
.getBrandWiseProductList(categorySlug, brandSlug, pageNo)
.listen((ProductListResponse response) {
if (_productList.value == null) {

totalPages = response.totalPage;
_productList.add(response.productList);

filterName.add('all');

_filteredProductList = Observable.combineLatest2(
_filterName, _productList, applyModelFilter)
.asBroadcastStream();
}
});

}
}

List<Product> applyModelFilter(
String filter,
List<Product> products,
) {
if (filter == 'all') {
return products;
} else {
return products
.where((seriesSLug) => seriesSLug.series.slug == filter)
.toList();
}
}

UI Widget类
 class _AllSeriesModelListScreenState extends State<AllSeriesModelListScreen> {
AllSeriesModelListScreenArguments allSeriesModelListScreenArguments;

ProductListScreenBloc bloc;

int _selectedSeriesChipValue = -1;
int _pageNo = 1;

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

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

@override
Widget build(BuildContext context) {
RouteSettings settings = ModalRoute.of(context).settings;
allSeriesModelListScreenArguments = settings.arguments;

_init();

return Scaffold(
body: CustomScrollView(
slivers: <Widget>[
StreamBuilder(
stream: bloc.filteredProductList,
builder: (context, snapshot) {
if (snapshot.hasData) {
List<Product> productList = snapshot.data;

return SliverPadding(
padding: EdgeInsets.symmetric(
vertical: 8.0,
horizontal: 10.0,
),
sliver: SliverGrid(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
crossAxisSpacing: 0.0,
mainAxisSpacing: 8.0,
),
delegate: SliverChildListDelegate(
buildModelGridList(productList),
),
),
);
} else {
return SliverList(
delegate: SliverChildListDelegate([
PaddingWithTitle(
title: 'No Model Available',
),
]),
);
}
})
],
),
);
}

void _init() {
if (null == bloc) {
bloc = ProductListScreenBloc(
AppProvider.getApplication(context),
);

bloc.loadBrandWiseProductList(
allSeriesModelListScreenArguments.categorySlug,
allSeriesModelListScreenArguments.brandSlug,
_pageNo);

}
}
}

最佳答案

我相信您错过了这两行内容。

final _filterName = BehaviorSubject<String>();
Sink<String> get filterName => _filterName;

您没有暴露水槽。 BehaviorSubject只是一个具有默认值的StreamController,并为最后一个值缓存。因此,作为每个Stream Controller ,它都有2个 Prop -水槽和小溪。推送数据,您需要访问接收器。
为此,您需要输入
StreamSink<String> get filterName => _filterName.sink;

加上为什么您在行为主题中没有种子值(value)?
必须具有该“默认”值
final _filterName = BehaviorSubject<String>(seedValue: '');

关于flutter - 为什么在Flutter Sink中添加数据不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59500366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com