1

我正在创建一个地图列表,每个地图有两个键["title"]["time"]所有键的值都在一个setState方法中提交,值通过按钮作为字符串提交,并且在列表中添加了一个地图对象,List _tasks = []即我想做的是访问列表中添加的最后一个 Map 的 ["time"] 元素并能够更改值。

这是一个应用小项目的一部分,帮助我了解更多关于 Flutter 的知识。应用程序存储库位于 Github 上https://github.com/jsdaniell/remindme

该列表被宣布:

List _tasks = [];

将地图对象添加到列表的函数:

void _addTask() {
    setState(
      () {
        Map<String, dynamic> newTask = Map();
        newTask["title"] = _taskController.text;
        newTask["time"] = _timeGlobal.toString();
        _taskController.text = "";
        _tasks.add(newTask);


      },
    );

  }

关键是我想每秒减去变量_timeGlobal的值1,全局范围内的int是什么,是["time"]在Map中设置为String的值,所以我写了这段代码:

  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();
    });
  }

我希望有一些方法可以访问地图列表中的最后一张地图,以及这张地图上的特定键。

4

1 回答 1

1

要访问列表中的最后一个元素,您可以使用: _tasks[_tasks.length - 1]; 因此,如果您想要列表最后一个元素内的 Map 中的“时间”键,请使用: var lastTime = _tasks[_tasks.length - 1]["time"];

于 2019-03-25T22:00:58.777 回答