0

I'm very new to dart, so don't judge me. :))

I just started to write a simple ToDo App in Dart. I want to add a button to the item, so i can delete it from the list. I add the button successfully, but don't get the click event working.

I know why the code isn't working, but don't know, what the best solution would be to solve this.

Some improvements would be awesome.

Thanks in advance Ron

my little sexy dartpad

1 Answer 1

1

You need to register the onClick listen to remove the current item on every button. Here's a working version of your code.

import 'dart:html';

InputElement toDoInput;
UListElement toDoList;

void main() {
  toDoInput = querySelector('#to-do-input');
  toDoList = querySelector('#to-do-list');

  toDoInput.onChange.listen(addToDoItem);
}

// Add item to list
void addToDoItem(Event e) {
  final toDoItem = new LIElement();
  toDoItem.text = toDoInput.value;

  final deleteItemButton = new ButtonElement()
    ..text = 'Delete'
    ..onClick.listen((_) => toDoItem.remove());

  toDoItem.children.add(deleteItemButton);
  toDoList.children.add(toDoItem);

  toDoInput.value = '';
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! That makes sense for me! :) Can you tell me the meaning of the underscore in listen((_) =>?
It's the name of the parameter of the function. Usually you use _ when you don't really need to use the parameter and you want to improve readibility of your code.
Ah, so no special meaning. thought so. :) Thanks for your help Alex :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.