1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Can't set null for bool dart flutter

Discussão em 'Mobile' iniciado por Stack, Outubro 10, 2024 às 12:52.

  1. Stack

    Stack Membro Participativo

    Currently I using Bloc Flutter to make a simple comment and reply but the issue show off when I do a function interaction (only like and dislike). I can't set null for bool. In this case the field isLike is store the value to demonstrate the interact of use whenever they like or not. The APIs are worked but when the case comment.isLike == true. I want to set the value of isLike is null but it doesn't work. Please help me to solve this issue, I take a lot of time to figure out but not found the solution yet.

    This is the model


    import 'package:move_app/data/models/user_model.dart';

    class CommentModel {
    final int? id;
    final DateTime? updatedAt;
    final DateTime? createdAt;
    final DateTime? deletedAt;
    final String? content;
    final int? numberOfLike;
    final UserModel? user;
    final String? timeConvert;
    final int? videoId;
    final bool? isLike;
    final int? totalDonation;

    CommentModel({
    this.id,
    this.updatedAt,
    this.createdAt,
    this.deletedAt,
    this.content,
    this.numberOfLike,
    this.user,
    this.timeConvert,
    this.videoId,
    this.isLike,
    this.totalDonation
    });

    factory CommentModel.fromJson(Map<String, dynamic> json) {
    return CommentModel(
    id: json['id'] as int?,
    updatedAt:
    json['updatedAt'] != null ? DateTime.parse(json['updatedAt']) : null,
    createdAt:
    json['createdAt'] != null ? DateTime.parse(json['createdAt']) : null,
    deletedAt:
    json['deletedAt'] != null ? DateTime.parse(json['deletedAt']) : null,
    content: json['content'] is String ? json['content'] : '',
    numberOfLike: json['numberOfLike'] as int?,
    user: json['user'] != null ? UserModel.fromJson(json['user']) : null,
    isLike: json['isLike'] as bool?,
    totalDonation: json['totalDonation'] as int?,
    );
    }

    CommentModel copyWith({
    int? id,
    DateTime? updatedAt,
    DateTime? createdAt,
    DateTime? deletedAt,
    String? content,
    int? numberOfLike,
    UserModel? user,
    String? timeConvert,
    bool? isLike,
    int? totalDonation
    }) {
    return CommentModel(
    id: id ?? this.id,
    updatedAt: updatedAt ?? this.updatedAt,
    createdAt: createdAt ?? this.createdAt,
    deletedAt: deletedAt ?? this.deletedAt,
    content: content ?? this.content,
    numberOfLike: numberOfLike ?? this.numberOfLike,
    user: user ?? this.user,
    timeConvert: timeConvert ?? this.timeConvert,
    isLike: isLike ?? this.isLike,
    totalDonation: totalDonation ?? this.totalDonation
    );
    }

    }

    here are the code for Bloc file


    void onVideoDetailLikeComment(
    VideoDetailLikeComment event,
    Emitter<VideoDetailState> emit,
    ) async {
    final comment = event.comment;

    var result;

    if (comment.isLike == null) {
    // If not liked yet
    final updatedComment = comment.copyWith(isLike: true);
    result = await commentRepository.postCommentReaction(updatedComment);
    emit(
    state.copyWith(
    listComments: updateCommentList(state.listComments, updatedComment),
    ),
    );
    } else if (comment.isLike == true) {
    result = await commentRepository.deleteCommentReaction(comment.id!);
    if (result.isRight()) {
    // Set isLike to null for unliking
    final updatedComment = comment.copyWith(isLike: null);
    emit(
    state.copyWith(
    listComments: updateCommentList(state.listComments, updatedComment),
    ),
    );
    }
    } else if (comment.isLike == false) {
    final updatedComment = comment.copyWith(isLike: true);
    result = await commentRepository.patchCommentReaction(updatedComment);
    emit(
    state.copyWith(
    listComments: updateCommentList(state.listComments, updatedComment),
    ),
    );
    }

    result.fold(
    (error) {
    print("Error Like : ========== : $error");
    },
    (response) {
    print("Success Like : ========== : $response");
    },
    );
    }

    This is the code for Event


    import 'package:equatable/equatable.dart';

    import '../../../../data/models/comment_model.dart';

    sealed class VideoDetailEvent extends Equatable {
    const VideoDetailEvent();
    }

    final class VideoDetailInitialEvent extends VideoDetailEvent {
    @override
    List<Object?> get props => [];
    }

    final class VideoDetailSelectQualityEvent extends VideoDetailEvent {
    final String selectedQuality;

    const VideoDetailSelectQualityEvent(this.selectedQuality);

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

    final class VideoDetailCommentChangedEvent extends VideoDetailEvent {
    final String? comment;

    const VideoDetailCommentChangedEvent({
    this.comment,
    });

    @override
    List<Object?> get props => [comment];
    }
    final class VideoDetailLikeComment extends VideoDetailEvent {
    final CommentModel? comment;

    const VideoDetailLikeComment({ this.comment});

    @override
    List<Object?> get props => [comment];
    }

    And this is State


    import 'package:equatable/equatable.dart';

    import '../../../../data/models/comment_model.dart';

    enum VideoDetailStatus {
    initial,
    processing,
    success,
    failure,
    }

    final class VideoDetailState extends Equatable {
    final VideoDetailStatus? status;
    final String? selectedQuality;
    final Map<String, String>? videoUrls;
    final String? inputComment;
    final List<CommentModel>? listComments;
    final Map<int, List<CommentModel>>? replies;
    final List<CommentModel>? listReplies;
    final int? lastCommentId;
    final CommentModel? commentModel;

    const VideoDetailState({
    this.videoUrls,
    this.status,
    this.selectedQuality,
    this.inputComment,
    this.listComments,
    this.replies,
    this.listReplies,
    this.lastCommentId,
    this.commentModel,
    });

    static VideoDetailState initial() => const VideoDetailState(
    status: VideoDetailStatus.initial,
    );

    VideoDetailState copyWith({
    VideoDetailStatus? status,
    String? selectedQuality,
    Map<String, String>? videoUrls,
    String? inputComment,
    List<CommentModel>? listComments,
    Map<int, List<CommentModel>>? replies,
    List<CommentModel>? listReplies,
    int? lastCommentId,
    CommentModel? commentModel,
    }) {
    return VideoDetailState(
    selectedQuality: selectedQuality ?? this.selectedQuality,
    status: status ?? this.status,
    videoUrls: videoUrls ?? this.videoUrls,
    inputComment: inputComment ?? this.inputComment,
    listComments: listComments ?? this.listComments,
    replies: replies ?? this.replies,
    listReplies: listReplies ?? this.listReplies,
    lastCommentId: lastCommentId ?? this.lastCommentId,
    commentModel: commentModel ?? this.commentModel);
    }

    @override
    List<Object?> get props => [
    status,
    selectedQuality,
    inputComment,
    listComments,
    replies,
    listReplies,
    lastCommentId,
    commentModel
    ];
    }

    Continue reading...

Compartilhe esta Página