因此,我目前有2个屏幕,一个屏幕有一个按钮,当用户按下按钮时,将显示第二个屏幕。没有什么太复杂了...
第二个屏幕应对集团发送的内容,并从本质上显示来自该群体发出的状态的数据。 所有功能都很好,并且可以正确显示vairables
fixedExpenseTotal
和
variableExpenseTtoal
,直到我增强了集团以散发出额外的状态为止; FetchedBalanceBoughtForwardState
。
现在看来,VairablesfixedExpenseTotal
和variableExpenseTtoal
没有被填充,它显示空字符串。似乎是,当我使bloc重新发射状态时(使用屏幕2上的临时按钮),小部件最初无法对状态做出反应,屏幕2上的小部件会正确地做出反应。因此,我很困惑为什么屏幕2上的小部件在加载窗口小部件时不会反应。...是因为在加载小部件之前已经发出了状态?值得仅将balance
领域添加为国家的一部分,以降低复杂性(尽管我希望各州/事件像适合的状态一样松散耦合)?尽管放置了FetchedExpendituresForCertainMonthState
语句,但我最初可以看到屏幕2加载时,它确实会摄取print
。
这是我的集团:
FetchedBalanceBoughtForwardState
这是屏幕1中的按钮的ONTAP处理程序:
FetchedExpendituresForCertainMonthState
这是对屏幕2上的群体发出的状态反应的小部件:
on<FetchForMonthExpenditureDefinitions>((event, emit) async {
//...business logic here
emit(FetchedExpendituresForCertainMonthState(
expendituresWithinTheMonth, monthToTraverse));
//Now we want to calculate the balance being bought forward from the previous month
add(CalculateBalanceBoughtForwardEvent(expenditures, from));
});
on<CalculateBalanceBoughtForwardEvent>((event, emit) async {
double balance = calculator.calculateBalanceBeingBoughtForward(event.expenditureDefinitions, event.dateOfBalanceBoughtForward);
print('Sending the balance bought forward as $balance');
emit(FetchedBalanceBoughtForwardState(balance));
});
当您做到
TextButton(
child: const Text('Finish!'),
onPressed: () async {
//Passing the context which contains the blocprovider to myApp
context.read<FinanceBloc>().add(
FetchForMonthExpenditureDefinitions(
DateTime.now()));
Navigator.of(context).push(MaterialPageRoute(
builder: (_) => BlocProvider.value(
value: BlocProvider.of<FinanceBloc>(context),
child: MyApp(),
),
));
},
)
return Container(
width: MediaQuery.sizeOf(context).width,
child: Container(
padding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 10.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16.0),
color: Colors.grey,
),
child: BlocBuilder<FinanceBloc, FinanceState>(
buildWhen: (previous, current) =>
previous != current && current is FetchedBalanceBoughtForwardState,
builder: (context, balanceState) {
double balanceBeingBoughtForward = balanceState is FetchedBalanceBoughtForwardState
? balanceState.balanceBoughtForward
: 0.0;
return BlocBuilder<FinanceBloc, FinanceState>(
buildWhen: (previous, current) =>
previous != current && current is FetchedExpendituresForCertainMonthState,
builder: (context, expendituresState) {
String fixedIncomeTotal = "";
String fixedExpenseTotal = "";
String variableExpenseTtoal = "";
String variableIncomeTotla = "";
if (expendituresState is FetchedExpendituresForCertainMonthState) {
expenditures = expendituresState.expenditures.values
.expand((list) => list)
.toList();
currentMonth = expendituresState.updatedDate;
fixedIncomeTotal = getFixedIncome(expenditures).toString();
fixedExpenseTotal = getFixedExpense(expenditures).toString();
variableExpenseTtoal = getVariableExpense(expenditures).toString();
variableIncomeTotla = getVariableIncome(expenditures).toString();
}
return Column(children: [
Text("Overview of expenditures for this month",
style: const TextStyle(fontSize: 24)),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Total fixed income: £$fixedIncomeTotal"),
Text("Total fixed expense: £$fixedExpenseTotal")
]
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Total variable income: £$variableIncomeTotla"),
Text("Total variable expense: £$variableExpenseTtoal")
]
),
Text("Balance bought forward: £$balanceBeingBoughtForward"),
Text("Click here for more intel!"),
]);
},
);
},
),
),
);
丢失了。由于locbuilder听到FetchedExpendituresForCertainMonthState
为
FetchedBalanceBoughtForwardState
,因此值不会被填充。最好的解决方案将仅使用一种状态并使用
expendituresState is FetchedExpendituresForCertainMonthState
发出新状态,以免丢失先前发射的状态值。另外,您应该尽量避免在任何地方筑巢false
,因为它可能会产生意外的结果。
使用freezed用于集体状态。它将为您生成
copyWith
方法,也让您将状态设置回
BlocBuilder
。虽然,您可以使用自己的
copyWith
实施。