为什么在Flutter Sink中添加数据无效?

时间:2019-12-27 11:49:00

标签: flutter dart rxdart

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

BLOC类

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窗口小部件类

 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);

   }
  }
}

2 个答案:

答案 0 :(得分:0)

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

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

您没有暴露水槽。 BehaviorSubject只是一个具有默认值的StreamController,并为最后一个值缓存。因此,作为每个Stream控制器,它都有2个道具-接收器和stream。推送数据,您需要访问接收器。 为此,您需要输入

StreamSink<String> get filterName => _filterName.sink;

加上为什么您在行为主题中没有种子值? 必须具有“默认”值

final _filterName = BehaviorSubject<String>(seedValue: '');

答案 1 :(得分:0)

只需将代码更改为此

def add_propertycheck(request, property_pk):
    property_id = property_pk
    data = {'property':property_id}
    tasks = Task.objects.filter(property=property_pk)
    category = tasks.values('category').distinct()
    tasks_name = tasks.values('name')
    category = tasks.values('category').distinct()
    property_reference = Property.objects.get(pk=property_id)
    if request.method == 'POST':
        taskform = TaskCheckForm(request.POST)
        for task in tasks:
            if form.is_valid():
                checkform = form.save()
                return HttpResponseRedirect(reverse('propertycheck:details', args=[pk]))
    else:
        taskform = TaskCheckForm()

    context = {
        'task_form':taskform,
        'title':"Add Property Check",
        'task':tasks,
        'reference':property_reference,
        'category':category,
    }
    return render(request, 'propertycheck/add-propertycheck.html', context)