在我的扑动应用程序中,我想每隔10秒检查一下我的api.我发现这篇文章每隔x个时间运行一个函数并执行以下操作:
class _MainPage extends State<MainPage> {
int starter = 0;
void checkForNewSharedLists(){
// do request here
setState((){
// change state according to result of request
});
}
Widget build(BuildContext context) {
Timer.periodic(Duration(seconds: 15), (Timer t) => checkForNewSharedLists());
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,请求堆积起来:在第一轮"重新启动应用程序"后,有两个请求到api,第二轮是4个请求,第三个是8个等等...
有人知道如何解决这个问题吗?
我在 Flutter 中得到以下信息List<Map>:
List<Map<String, dynamic>> recipeList = [
{
'name': 'rec1',
'id': 1,
'img': 'images/recipe.jpg',
'ingredients': [{
'name': 'salt',
'amount': '1',
'unit': '1',
},
{
'name': 'flour',
'amount': '100',
'unit': 'g',
},
{
'name': 'water',
'amount': '100',
'unit': 'g',
},
{
'name': 'milk',
'amount': '100',
'unit': 'g',
},],
},]
Run Code Online (Sandbox Code Playgroud)
我将它传递给几个Widgets,在某个时候我想将键值对添加{'didBuy':false}到成分列表中的每个Map(基本上是recipeList['ingredients'])。因此我呼吁:
List<Map<String, dynamic>> resultList = recipeList['ingredients'].map((elem) {
elem.addAll({'didBuy': false});
print(elem);
}).toList();
Run Code Online (Sandbox Code Playgroud)
不幸的是,出现以下错误消息:Dart Error: Unhandled exception:type '_InternalLinkedHashMap<String, bool>' is not a …