Insert element at the beginning of the list in dart

RopAli Munshi

I am just creating a simple ToDo App in Flutter. I am managing all the todo tasks in the list. I want to add any new todo task at the beginning of the list. I am able to use this workaround kind of thing to achieve that. Is there any better way to do this?

void _addTodoInList(BuildContext context){
    String val = _textFieldController.text;

    final newTodo = {
      "title": val,
      "id": Uuid().v4(),
      "done": false
    };

    final copiedTodos = List.from(_todos);

    _todos.removeRange(0, _todos.length);
    setState(() {
      _todos.addAll([newTodo, ...copiedTodos]);
    });

    Navigator.pop(context);
  }
CopsOnRoad

Use insert() method of List to add the item, here the index would be 0 to add it in the beginning. Example:

List<String> list = ["B", "C", "D"];
list.insert(0, "A"); // at index 0 we are adding A
// list now becomes ["A", "B", "C", "D"]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Add quotes at the beginning and end of each element in list using LINQ

From Dev

Insert element at the beginning of Ruby Hash?

From Dev

Is it possible to render a List<Element> in Polymer.dart?

From Dev

Insert string every nth element in a list of strings

From Dev

How to insert an element in the middle of a list?

From Dev

How to insert element at beginning of vector

From Dev

iTextSharp insert a page at the beginning

From Dev

Insert element to circular list using scheme

From Dev

Insert a new element in a specified position of a list

From Dev

Insert Element between each List element

From Dev

Jquery insert list element with sortable position

From Dev

Insert an element into a list in python

From Dev

Append element to beginning of list in Prolog

From Dev

Dart convert every element of list

From Dev

Insert element to list based on previous and next elements

From Dev

Java how to add insert element at beginning of variable argument array?

From Dev

Joining an element, beginning with small letter, to a previous element of the list

From Dev

Select sublists from python list, beginning and ending on the same element

From Dev

Dart sum list of lists by element

From Dev

Insert an list element into a document in MongoDB

From Dev

Insert Element in list during iteration

From Dev

How to insert an element in a sorted list in Java

From Dev

Insert an element into a List at index 0: "Cannot convert"

From Dev

findAndModify MongoDB insert element at beginning of array

From Dev

Insert sections at the beginning of UICollectionView

From Dev

Insert a word at beginning of line

From Dev

Insert element into a sorted Circular Double Linked List

From Dev

SML How to prepend an element to the beginning of every list in a list of lists?

From Dev

how to insert something in the beginning of an element?

Related Related

  1. 1

    Add quotes at the beginning and end of each element in list using LINQ

  2. 2

    Insert element at the beginning of Ruby Hash?

  3. 3

    Is it possible to render a List<Element> in Polymer.dart?

  4. 4

    Insert string every nth element in a list of strings

  5. 5

    How to insert an element in the middle of a list?

  6. 6

    How to insert element at beginning of vector

  7. 7

    iTextSharp insert a page at the beginning

  8. 8

    Insert element to circular list using scheme

  9. 9

    Insert a new element in a specified position of a list

  10. 10

    Insert Element between each List element

  11. 11

    Jquery insert list element with sortable position

  12. 12

    Insert an element into a list in python

  13. 13

    Append element to beginning of list in Prolog

  14. 14

    Dart convert every element of list

  15. 15

    Insert element to list based on previous and next elements

  16. 16

    Java how to add insert element at beginning of variable argument array?

  17. 17

    Joining an element, beginning with small letter, to a previous element of the list

  18. 18

    Select sublists from python list, beginning and ending on the same element

  19. 19

    Dart sum list of lists by element

  20. 20

    Insert an list element into a document in MongoDB

  21. 21

    Insert Element in list during iteration

  22. 22

    How to insert an element in a sorted list in Java

  23. 23

    Insert an element into a List at index 0: "Cannot convert"

  24. 24

    findAndModify MongoDB insert element at beginning of array

  25. 25

    Insert sections at the beginning of UICollectionView

  26. 26

    Insert a word at beginning of line

  27. 27

    Insert element into a sorted Circular Double Linked List

  28. 28

    SML How to prepend an element to the beginning of every list in a list of lists?

  29. 29

    how to insert something in the beginning of an element?

HotTag

Archive