강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

wkc0631님의 프로필 이미지
wkc0631

작성한 질문수

[코드팩토리] [중급] Flutter 진짜 실전! 상태관리, 캐시관리, Code Generation, GoRouter, 인증로직 등 중수가 되기 위한 필수 스킬들!

Restaurant Pagination -4 강의(7분) pState.data.last.id 제네릭 관련 질문

작성

·

4

·

수정됨

0


안녕하세요 강사님!

좋은 강의 덕분에 플러터를 계속 공부하면서 점점 1인분 개발자로 성장해가고 있는 것 같습니다 😊 항상 감사드립니다.

강의 중 궁금한 점이 있어 문의드립니다.
Restaurant Pagination -4 강의에서 7분쯤에 아래 코드가 나오는데요:

 

pState.data.last.id,

제가 이해하기로는 RestaurantStateNotifier에서 다루는 state가 CursorPaginationBase이고, fetchMore 상황에서는 CursorPaginationFetchingMore<T>타입이니, data는 List<T>가 될 텐데, 그렇다면 리스트의 마지막 인덱스는 T 인스턴스일 것이고, T에 id라는 멤버가 존재할지 안할지 확정되지 않았으니, 에러가 떠야한다고 생각하고 코드를 작성했습니다.
그런데 강의에서는 IDE에서 린트/에러 없이 pState.data.last.id 접근이 가능하더라고요. 저도 마찬가지로 해당 라인에 에러가 없는 상태입니다.
정확히 어떤 이유로 에러가 안 뜨는지 궁금합니다. 참고를 위해 아래 궁금증 발생 시점의 코드 스니펫 첨부 드립니다.

 

항상 좋은 강의 정말 감사합니다!

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:project/common/model/cursor_pagination_model.dart';
import 'package:project/common/model/pagination_params.dart';
import 'package:project/restaurant/repository/restaurant_repository.dart';

final restaurantProvider =
    StateNotifierProvider<
      RestaurantStateNotifier,
      CursorPaginationBase
    >((ref) {
      final repository = ref.watch(restaurantRepositoryProvider);
      final notifier = RestaurantStateNotifier(
        repository: repository,
      );
      return notifier;
    });

class RestaurantStateNotifier
    extends StateNotifier<CursorPaginationBase> {
  final RestaurantRepository repository;
  RestaurantStateNotifier({required this.repository})
    : super(CursorPaginationLoading()) {
    paginate();
  }

  void paginate({
    int fetchCount = 20,
    //추가로 데이터 더 가져오기
    //true - 추가로 데이터 더 가져옴
    //false - 새로고침(현재 상태를 덮어씌움)
    //
    bool fetchMore = false,
    //강제로 다시 로딩하기
    //true- CursorPaginationLoading()
    bool forceRefetch = false,
  }) async {
    //5가지 가능성
    //state의 상태
    //[상태가]
    //1) CursorPagination - 정상적으로 데이터가 있는 상태
    //2) CursorPaginationLoading - 데이터가 로딩중인 상태 (현재 캐시 없음)
    //3) CursorPaginationError - 에러가 있는 상태
    //4) CursorPaginationRefetching - 첫번째 페이지부터 다시 데이터를 가져올때
    //5) CursorPaginationFetchMore - 추가 데이터를 pagination 해오라는 요청을 받았을때

    //바로 반환하는 상황
    //1) hasMore == false(기존 상태에서 이미 다음 데이터가 없다는 값을 들고 있다면 )
    //2) 로딩중 - fetchMore == true
    // fetchMore가 아닐때 - 새로고침의 의도가 있을 수 있다.
    if (state is CursorPagination && !forceRefetch) {
      final pState = state as CursorPagination;
      if (!pState.meta.hasMore) {
        return;
      }
    }
    final isLoading = state is CursorPaginationLoading;
    final isRefetching = state is CursorPaginationRefetching;
    final isFetchingMore = state is CursorPaginationFetchingMore;
    //2번 반환 상황
    if (fetchMore &&
        (isLoading || isRefetching || isFetchingMore)) {
      return;
    }
    //PaginationParams 생성
    PaginationParams paginationParams = PaginationParams(
      count: fetchCount,
    );

    //fetchMore
    //데이터를 추가로 더 가져오는 상황

    if (fetchMore) {
      final pState = state as CursorPagination;
      state = CursorPaginationFetchingMore(
        data: pState.data,
        meta: pState.meta,
      );
      paginationParams = paginationParams.copyWith(
        after: pState.data.last.id,
      );
    }
  }
}

답변

답변을 기다리고 있는 질문이에요
첫번째 답변을 남겨보세요!
wkc0631님의 프로필 이미지
wkc0631

작성한 질문수

질문하기