在生成过程中调用了setState()或markNeedsBuild().重置提供程序时



此代码运行良好,正在重置提供程序元素,但控制台中出现此错误的原因。在有状态类的init((上调用此函数;

Future getPlotsGraphDataAccordingToCategoryAndDate(context,CategoryId,CategoryName,date) async {
var getRepresentationProvider =
Provider.of<RepresentationProvider>(context, listen: false);
getRepresentationProvider.resetPerticularCategoryDataAccordingDate();
var response = await http.get(url, headers : {
'accept': '*/*',
'Authorization': 'Bearer $token',
'Content-Type': 'application/json',
});
final body = jsonDecode(response.body.toString());
getRepresentationProvider.setCategoryName(CategoryName);
getRepresentationProvider.setCategoryId(CategoryId);
getRepresentationProvider.setperticularCategoryDataAccordingDate(
{
"TotalInstallmentofplottypeCount":body["data"]["TotalInstallmentofplottypeCount"],
"paidInstallmentofplottypeCount": body["data"]["paidInstallmentofplottypeCount"],
"UnpaidInstallmentofplottypeCount":body["data"]["UnpaidInstallmentofplottypeCount"],
"totalAmountofInstallment": body["data"]["totalAmountofInstallment"],
"PaidtotalAmountofInstallment": body["data"]["PaidtotalAmountofInstallment"],
"UnPaidtotalAmountofInstallment":body["data"]["UnPaidtotalAmountofInstallment"]
}
).then((value) {
print("Here is data Set ${body}");
});
return body;
}

提供程序代码如下,其中设置并重置了相关类别数据AccordingDate:

class RepresentationProvider extends ChangeNotifier {
List perticularCategoryDataAccordingDate = [];

Future setperticularCategoryDataAccordingDate(data) async {
perticularCategoryDataAccordingDate.add(data);
notifyListeners();
print("object here $perticularCategoryDataAccordingDate");
}


Future resetPerticularCategoryDataAccordingDate() async {
perticularCategoryDataAccordingDate = [];
notifyListeners();
}

}

试试这个

void initState() {
super.initState();
WidgetsBinding.instance
.addPostFrameCallback((_) => getPlotsGraphDataAccordingToCategoryAndDate(context,CategoryId,CategoryName,date);
}

最新更新