1

Why static keyword is used? Why passing a '-' argument for User?

import 'package:equatable/equatable.dart';

class User extends Equatable {
  const User(this.id);

  final String id;

  @override
  List<Object> get props => [id];

  *static const empty = User('-');*
}
Nikash Deka
  • 427
  • 2
  • 4
  • 15
  • Did you ask the person who wrote this? It's a choice this person made. We cannot say why a minus was chosen. What connection does flutter-bloc have to this code? – nvoigt Mar 14 '21 at 09:40
  • 1
    It's `static` because it's `const` ([`const` members must be `static`](https://stackoverflow.com/q/17074897/)). – jamesdlin Mar 14 '21 at 09:45
  • What happens if you remove the line? If something does not work any longer, it may give you a hint what this is used for. If everything works when removing that line, it probably wasn't really important and is indeed not useful for your program. – nvoigt Mar 14 '21 at 09:45
  • This snippet is from the flutter_login example written by the Author of the bloc library itself! – Nikash Deka Mar 14 '21 at 10:17
  • 1
    A possible motivation: declaring `empty` as **`static const`**, it is possible to use `empty` as **default value for a named parameter**. For example: `void logIn({User user = User.empty}) {}`. However, the use of '-' for the `id` is an arbitrary choice. – Mabsten Mar 14 '21 at 10:43
  • It is not related to the subclassing of Equatable (which serves to provide the class of an implementation of `hashCode` and `==`) – Mabsten Mar 14 '21 at 10:52

0 Answers0