【发布时间】:2021-03-17 20:46:01
【问题描述】:
作为培训的一部分,我目前正在进行一个项目。我需要创建一个调度 Flutter 应用程序。但是,我在更新要显示的列表时遇到问题。
我有一个名为 handleRefresh 的方法:在初始化时加载列表(在 initState() 中调用),当我使用 refreshIndicator 刷新页面时。
列表在启动时加载良好,但随后修改时,刷新方法不起作用。
为了刷新显示,我清除列表,然后添加新元素。但是,它不起作用。 如果我决定不清除列表而只添加元素,它可以工作,但我有重复项,因为我在添加新元素之前没有清除列表。
那么,你知道为什么清除列表会阻止它被刷新吗?
对不起,我的英语不太好,我是法国人
class _PageControlesState extends State<PageControles>
with AutomaticKeepAliveClientMixin<PageControles> {
List<SemaineCc> _listeSemaineCc = new List<SemaineCc>();
Temp body;
List<Widget> _semaineCcWrapper;
final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
new GlobalKey<RefreshIndicatorState>();
@override
void initState() {
super.initState();
_semaineCcWrapper = new List<Widget>();
_semaineCcWrapper.add(new Align(
alignment: Alignment.center,
child: CircularProgressIndicator(),
));
_handleRefresh(debut: true);
}
@override
// ignore: must_call_super
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
title: Container(
margin: EdgeInsets.only(top: 50, bottom: 30),
width: double.infinity,
child: Text(
'Contrôles',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.w600,
color: ThemeProvider.themeOf(context).data.textTheme.headline1.color,
)
),
),
),
body: RefreshIndicator(
key: _refreshIndicatorKey,
onRefresh: _handleRefresh,
child: Container(
width: MediaQuery.of(context).size.width,
child: SingleChildScrollView(
physics: const AlwaysScrollableScrollPhysics(),
child: ConstrainedBox(
constraints: BoxConstraints(
minHeight: MediaQuery.of(context).size.height-30-50-30-27,
),
child: Align(
alignment: Alignment.topCenter,
child: Container(
margin: EdgeInsets.all(5),
width: double.infinity,
child: Container(
margin: EdgeInsets.only(
bottom: 60
),
padding: EdgeInsets.symmetric(vertical: 0, horizontal: 0),
width: double.infinity,
child: Column(
children: _semaineCcWrapper,
)
),
),
),
),
)
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) => PopupAjouterControleUI(),
).then((value) {
print(value);
if(value!=null)
ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('sauvegarde reussie')));
_handleRefresh();
});
},
child: const Icon(Icons.add),
backgroundColor: Colors.blueAccent,
),
);
}
Future<void> _handleRefresh({bool debut = false}) {
return fetchCc().then((value) {
setState(() {
_semaineCcWrapper.clear();
_listeSemaineCc = value;
for (SemaineCc semaineCc in value) {
_semaineCcWrapper.add(SemaineCcUI(semaineCc: semaineCc));
}
});
});
}
@override
bool get wantKeepAlive => true;
}
【问题讨论】:
-
欢迎来到 Stack Overflow!你能编辑你的帖子并添加一个来自
fetchCc()的数据的例子吗?刷新操作之前和之后。另外,为什么你需要GlobalKey?你似乎没有使用它。 -
谢谢你的回答,我昨天解决了这个问题。我会发布我的补丁。再次感谢!!!