Reputation: 410
I'm create a list of maps, each map have two keys ["title"]
and ["time"]
, all the values of keys is submitted in a setState
method, with the values submited as Strings by a button, and a map object is added inside the list, that's List _tasks = []
, so what I would like to do is access the ["time"] element of the last Map added in the list and be able to change the value.
This is part of a little project of application, to help me to learn more about Flutter. The repository of application is on Github on https://github.com/jsdaniell/remindme
The list is declared:
List _tasks = [];
The function that's adding a map object to the list:
void _addTask() {
setState(
() {
Map<String, dynamic> newTask = Map();
newTask["title"] = _taskController.text;
newTask["time"] = _timeGlobal.toString();
_taskController.text = "";
_tasks.add(newTask);
},
);
}
The point is that I want to subtract a value of 1 every second of variable _timeGlobal, what's a int in the global scope and is the value that is set as a String in the ["time"]
in the Map, so I write this code:
String countdownTime(Map newTask) {
Timer timer = new Timer.periodic(new Duration(seconds: 1), (timer) {
// Here I want to access the value of ["time"] element, where's in the last position of list and update the value.
_saveData();
return Home();
});
// Stop the periodic timer using another timer which runs only once after specified duration
new Timer(new Duration(minutes: _timeGlobal), () {
timer.cancel();
});
}
I hope that have some way to access the last map in a list of maps, and a specific key on this map.
Upvotes: 1
Views: 2364
Reputation: 1422
to access to the last element in the list you can use :
_tasks[_tasks.length - 1];
so if you want the "time" key from the Map thats inside the last element of your list use :
var lastTime = _tasks[_tasks.length - 1]["time"];
Upvotes: 1