Janski
Janski

Reputation: 33

Flutter: The default value of an optional parameter must be constant

My IDE gives me the error:

The default value of an optional parameter must be constant.

When I try to use this class in an Riverpod StateProvider as a state. If the Uint8List is a problem, I could substitute it with an string. How can I alter the class to make the default values constant?

import 'dart:typed_data';

/// [NoSignalUser]
///  A normal user model containing all the neccessary data to be used in the app
/// This model will contains as described below
class NoSignalUser {
  final String id;
  final String name;
  final String email;
  late String? bio;
  late Uint8List? image;
  late String? imgId;
  NoSignalUser({
    required this.id,
    required this.name,
    required this.email,
    this.bio ,
    this.image,
    this.imgId,
  });

  NoSignalUser copyWith({
    String? id,
    String? name,
    String? email,
    String? bio,
    Uint8List? image,
    String? imgId,
  }) {
    return NoSignalUser(
      id: id ?? this.id,
      name: name ?? this.name,
      email: email ?? this.email,
      bio: bio ?? this.bio,
      image: image ?? this.image,
      imgId: imgId ?? this.imgId,
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'name': name,
      'email': email,
      'bio': bio,
      'imgId': imgId,
    };
  }

  factory NoSignalUser.fromMap(Map<String, dynamic> map) {
    return NoSignalUser(
      id: map['id'],
      name: map['name'],
      email: map['email'],
      bio: map['bio'],
      imgId: map['imgId'],
      image: map['image'],
    );
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;

    return other is NoSignalUser &&
        other.id == id &&
        other.name == name &&
        other.email == email &&
        other.bio == bio &&
        other.image == image &&
        other.imgId == imgId;
  }

  @override
  int get hashCode {
    return id.hashCode ^
    name.hashCode ^
    email.hashCode ^
    bio.hashCode ^
    image.hashCode ^
    imgId.hashCode;
  }
}

Upvotes: 0

Views: 539

Answers (1)

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63749

These fields aren't final, that's you can't make const constructor.

  late String? bio;
  late Uint8List? image;
  late String? imgId;

To make const all fields are needed to be final, you can do it like

class NoSignalUser {
  final String id;
  final String name;
  final String email;
  final String? bio;
  final Uint8List? image;
  final String? imgId;

  const NoSignalUser({
    required this.id,
    required this.name,
    required this.email,
    this.bio,
    this.image,
    this.imgId,
  });

Upvotes: 0

Related Questions