在Flutter中应如何构建BloC?

时间:2019-05-23 12:31:35

标签: dart flutter structure bloc

我是BloC和Flutter的新手。对于单个简单屏幕,它应该可以正常工作。但是让我们看一下我的情况,我对如何使用BloC模式感到困惑。

我有一个名为Container的屏幕,其中包含PageView屏幕中的Content。假设我在该PageView中有5页。这些页面计数是动态的。这些页面只是数据不同。

我正在考虑两种实现方法:

1 /使用一个单人团体并将其传递给我的5个孩子Content

2 /为Container使用一个集团,为Content使用另一个集团。因此,这似乎是嵌套的集团。 ContainerBloc将包含ContentBloc的列表。

第一种方法。我看到的问题是重新渲染问题。我将创建每个页面的数据列表:

List<List<String>> allData = [];
BehaviorSubject<List<List<String>>> _allData = BehaviorSubject<List<List<String>>>();

Observable<List<String>> getData(index) => _allData.stream.map((list) => list[index]); //This stream returns the list at the index

,每个页面将通过以下方式监听数据:

//StreamBuilder in the UI
stream: widget.bloc.getData(index);

和数据的更新方法应类似于:

void updateData(int index, List<String> newData) {
    List<String> temp = allData[index];
    temp.add(newData);
    allData[index] = temp;
    _allData.sink.add(allData);
}

据我了解,一旦更新了一页。所有其他页面都会重新渲染,因为它们都收听getData(index)触发的_allData.sink.add(allData);流 因此,我认为即使该页面的数据没有更改,所有页面也会被重新渲染。

第二种方法。我不知道嵌套这样的集团是否是最佳实践。在某些情况下,ContainerBloc必须侦听某些ContentBloc输出。

我现在有点困惑。

谢谢您的时间。

0 个答案:

没有答案
相关问题