我正在学习 Flutter 和 GetX。我有模型:
class SectionsDBStat {
int notificationsTotalCount;
int notificationsWaitingForProcessing;
DateTime notificationsLastArchiveDate;
SectionsDBStat({
required this.notificationsTotalCount,
required this.notificationsWaitingForProcessing,
required this.notificationsLastArchiveDate
});
factory SectionsDBStat.fromJson(Map<String, dynamic> json) {
return SectionsDBStat(
notificationsTotalCount: json['notificationsTotalCount'],
notificationsWaitingForProcessing: json['notificationsWaitingForProcessing'],
notificationsLastArchiveDate: DateTime.parse(json['notificationsLastArchiveDate'])
);
}
}
我正在尝试创建从 http 接收数据的 Getx 控制器:
class SectionsStatController extends GetxController {
SectionsDBStat sectionsDBStat = Future.value(<SectionsDBStat>).obs;
getStat() async {
var value = await http.get('${url}/stat?total');
if(value.statusCode == 200) {
Map<String, dynamic> valueMap = jsonDecode(value.body)['data'];
sectionsDBStat = SectionsDBStat.fromJson(valueMap);
update();
} else {
print("Can't get stat: ${value.statusCode}");
}
}
}
我收到一个错误:
A value of type 'Rx<Future<List<SectionsDBStat>>*>*' can't be assigned to a variable of type 'SectionsDBStat'.
所以我应该使用另一种类型。但是什么?我试过final
但得到错误:
'sectionsDBStat' can't be used as a setter because it's final.