我正在尝试在Flutter应用中实现BLoC模式,
基本上,此应用会计算一些结果并将其显示在表格中。
我已经创建了CalculationResultProvider
和CalculationResultBloc
如下
class CalculationResultProvider { List<EstimationResult> resultList = new List(); List<EstimationResult> calculateResult(){ return getInitialData(); } List<EstimationResult> getInitialData(){ var cement = new EstimationResult(); cement.material = "Cement"; cement.unit = "Ton"; cement.qty = 10; var sand = new EstimationResult(); sand.material = "Sand"; sand.unit = "Ton"; sand.qty = 12; var gravel = new EstimationResult(); gravel.material = "Gravel"; gravel.unit = "Ton"; gravel.qty = 5; var steel = new EstimationResult(); steel.material = "Steel"; steel.unit = "Ton"; steel.qty = 5; List<EstimationResult> resultList = new List(); resultList.add(cement); resultList.add(sand); resultList.add(gravel); resultList.add(steel); return resultList; } }
和我的BLoC提供程序类如下
class CalculationResultBloc {
final resultController = StreamController(); // create a StreamController
final CalculationResultProvider provider =
CalculationResultProvider(); // create an instance of our CounterProvider
Stream get getReult =>
resultController.stream; // create a getter for our stream
void updateResult() {
provider
.calculateResult(); // call the method to increase our count in the provider
resultController.sink.add(provider.resultList); // add the count to our sink
}
void dispose() {
resultController.close(); // close our StreamController
}
}
然后我需要在表格小部件中显示此数据
class ResultTableWidget extends StatefulWidget {
@override
State<StatefulWidget> createState() => ResultTableWidgetState();
}
class ResultTableWidgetState extends State {
final bloc =
CalculationResultBloc(); // create an instance of the counter bloc
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: bloc.getReult,
initialData: CalculationResultProvider().getInitialData(),
builder: (context, snapshot) {
DataTable(
columns: [
DataColumn(label: Text('Patch')),
DataColumn(label: Text('Version')),
DataColumn(label: Text('Ready')),
],
rows:
'${snapshot.data}' // Loops through dataColumnText, each iteration assigning the value to element
.map(
((element) => DataRow(
cells: <DataCell>[
DataCell(Text(element[
"Name"])), //Extracting from Map element the value
DataCell(Text(element["Number"])),
DataCell(Text(element["State"])),
],
)),
)
.toList(),
);
});
}
@override
void dispose() {
bloc.dispose();
super.dispose();
}
}
要迭代返回表,它应该是List<EstimationResult>
但是如何将快照转换为List<EstimationResult>
?
在bloc类或小部件类中进行转换的最佳位置在哪里?
飞镖和飞镖不熟悉,任何人都可以回答我的问题吗?
谢谢。
答案 0 :(得分:2)
您的窗口小部件类将不了解StreamBuilder
中stream函数给出的数据类型,有很多方法可以在流传输BloC
之前转换数据,但是所有这些方法都会之所以没有用,是因为对于窗口小部件类而言,它只是一个snapshot
,并且在编译时只能访问的字段是应用于data
字段等通用快照的字段。因此,访问自定义列表字段的唯一方法是为您的StreamBuilder
函数提供其stream
函数所需的数据类型:
StreamBuilder<List<EstimationResult>>(
stream: bloc.getReult,
initialData: CalculationResultProvider().getInitialData(),
builder: (context, snapshot) {
//...
}
);
通过这种方式,您可以将snapshot
视为List<EstimationResult>
,并在收到实际快照之前立即访问内部字段和函数。在您的情况下,您应该将EstimationResult
类导入窗口小部件类。