嗨,我的提供程序中有一个方法可以从服务器返回项目列表:
Future<void> getServerMeals(String userName, DateTime date) async {
final QueryBuilder<ParseObject> parseQuery =
QueryBuilder<ParseObject>(ParseObject('UsersEaten'));
parseQuery
..whereContains('eatenBy', userName)
..whereGreaterThanOrEqualsTo('createdAt', date);
final ParseResponse apiResponse = await parseQuery.query();
if (apiResponse.success && apiResponse.results != null) {
List<dynamic>? apiRes = apiResponse.results;
List<dynamic> newMeals = apiRes!
.map((e) => EatenItem(
id: e['objectId'],
eatenCal: e['eatenCal'],
eatenTitle: e['eatenTitle'],
eatenImageUrl: e['eatenImg'],
userId: e['objectId'],
))
.toList();
_eatenMeals = newMeals;
print(apiResponse.results);
}
}
在我的屏幕上我有一个 FutureBuilder 并且我已经将该方法保存为未来:
DateTime now = DateTime.now();
Container(
height: MediaQuery.of(context).size.height * 0.6,
margin: EdgeInsets.only(left: 30, right: 30),
child: FutureBuilder(
builder: (ctx, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
return ListView.builder(
itemCount: mealsList.eatenMeals.length,
itemBuilder: (ctx, index) =>
DailyEaten(mealsList.eatenMeals[index]),
);
},
future: Provider.of<EatenMeals>(context).getServerMeals(
currentUser.username, DateTime(now.year, now.month, now.day)),
),
),
在 jalali_calendar 1.0.4 的帮助下,我还有一个日期选择器,它允许用户更改日期并从特定日期获取项目:
Future<dynamic> _selectDate() async {
String? picked = await jalaliCalendarPicker(
context: context, convertToGregorian: true);
final newDate = DateTime.parse(picked!);
setState(() {
now = newDate;
print(now);
});
}
但是当我调用这个方法时,我的列表不会改变,我有刷新屏幕但我的数据是一样的。
我可以在控制台中看到“现在”变量的变化,但来自 FutureBuilder 的未来列表仍然相同。