0

I am porting an angular application to angulardart and I am encountering some challenges when it comes to the EventEmitter.

My current .ts file looks like this:

import { Component, EventEmitter } from "@angular/core";

@Component({
  selector: "app-post-create",
  templateUrl: "./post-create.component.html",
  styleUrls: ["./post-create.component.css"]
})

export class PostCreateComponent {
  enteredTitle = "";
  enteredContent = "";
  postCreated = new EventEmitter();

  onAddPost() {
    const post = {
      title: this.enteredTitle,
      content: this.enteredContent
    };
    this.postCreated.emit(post);
  }
}

According to an older post, EventEmitter is deprecated and shouldn't be used.

My current .dart file looks like this:

import 'dart:async';

import 'package:angular/angular.dart';
import 'package:angular_components/angular_components.dart';
import 'package:angular_forms/angular_forms.dart';

@Component(
  selector: 'app-post-create',
  styleUrls: ['post_create_component.css'],
  templateUrl: 'post_create_component.html',
  directives: [
    MaterialButtonComponent,
    materialInputDirectives,
    formDirectives
  ],
  providers: [],
)
class PostCreateComponent {
  String enteredTitle = '';
  String enteredContent = '';
  final _postCreatedController = new StreamController<String>();

  @Output
  Stream<String> get postCreated => _postCreatedController.stream;

  onAddPost() {
    Map<String, String> post = {
      'title': this.enteredContent,
      'content': this.enteredContent
    };
  }
}

My dart file needs to be fixed. I am not using the Stream correctly. Any help is appreciated.

NiceSuites
  • 65
  • 4

1 Answers1

1

Following worked for me. You can use StreamController.broadcast().

You can then add your object to the add() method.

@Component(
  selector: 'new-post',
  directives: [coreDirectives, 
                formDirectives,
                FileUploader,
                materialInputDirectives,
                MaterialButtonComponent],
  templateUrl: 'post_new_component.html',
  styleUrls: ['post_new_component.css'],
  providers: [ClassProvider(PostService)]
)
class PostNewComponent {
  final PostService _postService;
  final _onDone = new StreamController.broadcast();

  String postText;
  Post post;

  @Input()
  Place place;

  @Output()
  Stream get doneIt => _onDone.stream;

  PostNewComponent(this._postService);

  Future<void> save() async {
    await _postService.create(postText,place.id).then(((_) => _onDone.add(1)));
  }
}

In the html you can then specify the "doneIt" event, which triggers a function.

<new-post (doneIt)="loadPosts()" [place]="place"></new-post>

In the parent dart component file you can then add a function like this:

 Future<void> loadPosts() async {
    print("broadcast received");
  }

I think you can use loadPosts($event) to pass the event, but that's something you would still have the check by yourself.

Hope it helps. Had a similar question posted here:

How to notify parent component of change and refresh view

Christopher Armstrong
  • 3,477
  • 6
  • 34
  • 40