0

I am following Felix Angelov's tutorial "https://www.hidigital.io/blog/2020/06/flutter-login-tutorial-with-flutter-bloc" on Flutter Bloc pattern.

Why is the class for AuthenticationEvent instantiated like this:

import 'package:meta/meta.dart';
import 'package:equatable/equatable.dart';

abstract class AuthenticationEvent extends Equatable {
  AuthenticationEvent([List props = const []]) : super(props);  <--------- this line
}

class AppStarted extends AuthenticationEvent {
  @override
  String toString() => 'AppStarted';
}

class LoggedIn extends AuthenticationEvent {
  final String token;

  LoggedIn({@required this.token}) : super([token]);

  @override
  String toString() => 'LoggedIn { token: $token }';
}

class LoggedOut extends AuthenticationEvent {
  @override
  String toString() => 'LoggedOut';
}

While for the LoginEvent class it is instantiated like this:

import 'package:meta/meta.dart';
import 'package:equatable/equatable.dart';

abstract class LoginEvent extends Equatable {
  const LoginEvent(); <----------------------------------------- this line
}

class LoginButtonPressed extends LoginEvent {
  final String username;
  final String password;

  const LoginButtonPressed({
    @required this.username,
    @required this.password,
  });

  @override
  List<Object> get props => [username, password];

  @override
  String toString() =>
      'LoginButtonPressed { username: $username, password: $password }';
}

What is the difference here?

Manas
  • 3,060
  • 4
  • 27
  • 55

1 Answers1

1

The AuthenticationEvent is written with an older version of the Equatable library. You cannot use that syntax in the current version.

Derek K
  • 2,756
  • 1
  • 21
  • 37
  • Thank you! do you mind explaining what is actually hapening in the old syntax please? would like to just understand it – Manas Sep 30 '20 at 14:17
  • The idea is the same. But instead of passing properties to the props getter, you pass them to the variable arguments constructor, which by default is initialized with empty list const []. – Derek K Sep 30 '20 at 14:28