강의

멘토링

커뮤니티

Cộng đồng Hỏi & Đáp của Inflearn

Hình ảnh hồ sơ của gmocca
gmocca

câu hỏi đã được viết

[Ứng dụng Block] Tạo ứng dụng thực tế (app review sách): đăng nhập SNS, ứng dụng Firebase, quản lý trạng thái Bloc, GoRouter

Triển khai tính năng theo dõi

즐겨찾기 기능 질문 요청드립니다..

Đã giải quyết

Viết

·

480

·

Đã chỉnh sửa

1

페이지 처음진입시 즐겨찾기 클릭하면

정상적으로 별이 on 으로 바뀌게 되는데

" 한번끄고 다시 키면 on으로 바뀌지않고 off 형태의 아이콘이

되어있고 다시 뒤로갔다가 들어오면 정상적으로 on 으로 바껴있습니다 !!

= 파이어베이스 db 상에는 데이터가 들어오지만

상태가 바뀌지않아서 off 상태일때 10번누르면

10번이 다 firebase db에 insert 되는현상이 나타납니다 ㅠㅠ

profilePage

actions: [
  GestureDetector(
    onTap: () {
      var myUid = context.read<AuthenticationCubit>().state.user!.uid;
      context.read<UserProfileCubit>().followToggleEvent(myUid!);
    },
    child: Padding(
      padding: const EdgeInsets.only(right: 20),
      child: BlocBuilder<UserProfileCubit,UserProfileState>(
        builder: (context,state) {
          var myUid = context.read<AuthenticationCubit>().state.user!.uid;
          var isFollowing = state.userModel?.followers?.contains(myUid) ?? false;
          return SvgPicture.asset(isFollowing ? 'assets/svg/icons/icon_follow_on.svg' :'assets/svg/icons/icon_follow_off.svg');
        }
      ),
    ),

user profile cubit

  void followToggleEvent(String myUid) async {
    if (state.userModel!.followers != null &&
        state.userModel!.followers!.contains(myUid)) {
      // 즐겨찾기 취소 언팔
      var result = await userRepository.followEvent(false,state.userModel!.uid!,myUid);
      if (result) {
        await _unfollow(myUid);
      }

    } else {
      // 즐겨 찾기 하기
      var result = await userRepository.followEvent(true,state.userModel!.uid!, myUid);
      if (result) {
        await  _follow(myUid);
      }
    }
  }

  _unfollow(myUid) async {
    emit(
     await state.copyWith(
        userModel: state.userModel!.copyWith(
          followers: List.unmodifiable(
            [...state.userModel!.followers!.where((targetUid) => targetUid != myUid)],
          ),
        ),
      ),
    );
  }

  _follow(myUid) async {
    print(state.userModel!.followers);
    if (state.userModel!.followers == null) {
      // 최초 팔로워 대상
      emit(
        await state.copyWith(
          userModel: state.userModel!.copyWith(
            followers: List.unmodifiable(
              [myUid],
            ),
          ),
        ),
      );
    } else {
      // 다른사람이 이미 팔로워 한사람
      emit(
        await state.copyWith(
          userModel: state.userModel!.copyWith(
            followers: List.unmodifiable(
              [
                ...state.userModel!.followers!,
                uid
              ],
            ),
          ),
        ),
      );
    }
  }
}

user-repository

Future<bool> followEvent(bool isFollow,String targetUid, String myUid) async {
  try {
    // 2 가지 업데이트 ( 트랜잭션 실행 필요 )
    final batch = db.batch();

    // Type1 . 상대방 팔로워에 내가 들어가는건다..
    var targetUserDoc =
    await db.collection("users").where("uid", isEqualTo: targetUid).get();
    UserModel targetUserInfo = UserModel.fromJson(targetUserDoc.docs.first.data());
    var followers = targetUserInfo.followers ?? []; // 최초사람

    if (isFollow) {
    followers.add(myUid);
    } else {
    followers.remove(myUid);
    }

    var targetRef = db.collection("users").doc(targetUserDoc.docs.first.id);
    batch.update(targetRef, {'followers': followers});

    // Type1 . 내 팔로워에 상대방이 들어간다...
    var myUserDoc =
    await db.collection("users").where("uid", isEqualTo: myUid).get();
    UserModel myUserInfo = UserModel.fromJson(myUserDoc.docs.first.data());
    var followings = myUserInfo.followings ?? [];

    if (isFollow) {
    followings.add(targetUid);
    } else {
    followings.remove(targetUid);
    }

    var MyRef = db.collection("users").doc(myUserDoc.docs.first.id);
    batch.update(MyRef, {'followings': followings});

    await batch.commit();
    return true;
  } catch (e) {
    return false;
  }
}

 

 

flutterfirebasebloc

Câu trả lời 1

0

gmocca님의 프로필 이미지
gmocca
Người đặt câu hỏi

이게 계속 찾아본 결과 !!

파이어베이스 db 스토리지와 통신하는데

약간의 텀이있어서 즉각즉각 반영이 안되었던거였습니다 !!

감사합니다 ㅠㅠ

Hình ảnh hồ sơ của gmocca
gmocca

câu hỏi đã được viết

Đặt câu hỏi