강의

멘토링

커뮤니티

Inflearn Community Q&A

wer061032192's profile image
wer061032192

asked

[Code Factory] [Intermediate] Flutter Real Practice! State Management, Cache Management, Code Generation, GoRouter, Authentication Logic, etc. Essential Skills to Become an Intermediate!

Caching RestaurantDetail

state 에 data를 변경할때 관련 질문드립니다.

Resolved

Written on

·

346

·

Edited

1

안녕하세요. 좋은 강의 감사합니다.

restaurant_provider.dart의 RestaurantStateNotifier getDetail() 메소드에서

 

state = pState.copyWith(
      data: pState.data
          .map<RestaurantModel>((e) => e.id == id ? resp : e)
          .toList(),
    )

이 구문은 array 전체를 돌아야되는 반면에

pState.data[pState.data.indexWhere((element) => element.id == id)] = resp;
state = pState.copyWith(
  data: pState.data,
);

이 구문은 array를 index를 찾을때까지만 돌면되서 더욱 효율적인것이 아닌가 싶어서 혹시 위와같이 map으로 로직을 구성하신 이유가 있는지 궁금합니다.

flutter하이브리드-앱

Answer 1

0

codefactory님의 프로필 이미지
codefactory
Instructor

안녕하세요!

그렇게 하시면 마지막 값이 아니라는 가정하에 말씀하신대로 조금 더 효율적입니다.

알고리즘의 관점에서는 둘 다 BIg O 콘스탄트이기 때문에 큰 차이는 없습니다.

저같은경우 map 이 조금 더 깔끔하게 보인다는 관점을 갖고 있습니다.

감사합니다!

wer061032192's profile image
wer061032192

asked

Ask a question