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

SY. Jeoung님의 프로필 이미지

작성한 질문수

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

Provider안에 Provider 사용하기

Provider 와 StateProvider의 사용에 관해.

해결된 질문

23.11.29 08:24 작성

·

393

·

수정됨

0

강의 잘 듣고 있습니다. 한 가지 의문이 있어 글 올립니다. 강의 내용 중 provider.dart에서

final filterShoppingListProvider = Provider<List<ShoppingListItem>>((ref) { } 부분에서

final filterShoppingListProvider = StateProvider<List<ShoppingListItem>>((ref) { } 로 사용하면 어떻게 다를까요?
Provider 안에 속한 Provider를 인지하기 위해 꼭 Provider를 사용한다는 걸까요?...

1회성은 StateProvider를 쓴다는 식으로 이해하면 되겠습니까?

 

답변 2

0

SY. Jeoung님의 프로필 이미지
SY. Jeoung
질문자

2023. 11. 29. 09:25

알겠습니다. 빠른 답변 감사드립니다.

0

코드팩토리님의 프로필 이미지
코드팩토리
지식공유자

2023. 11. 29. 08:57

안녕하세요!

Provider는 캐싱을 위해서 사용합니다. 즉, 특정 값을 들고있는 목적입니다.

StateProvider는 Provider + 단순한 형태의 변경을 하는 Provider입니다.

예를들어서 정수 값을 들고있다가 해당 값을 변경하고싶다면 StateProvider를 사용하는게 맞고 단순히 특정 인스턴스를 캐싱해두고싶다면 Provider를 사용하는게 맞습니다.

Riverpod 본문도 첨부해드립니다.

 

Provider is typically used for:

  • caching computations

  • exposing a value to other providers (such as a Repository/HttpClient).

  • offering a way for tests or widgets to override a value.

  • reducing rebuilds of providers/widgets without having to use select.

 

StateProvider is a provider that exposes a way to modify its state. It is a simplification of NotifierProvider, designed to avoid having to write a Notifier class for very simple use-cases.

StateProvider exists primarily to allow the modification of simple variables by the User Interface.
The state of a StateProvider is typically one of:

  • an enum, such as a filter type

  • a String, typically the raw content of a text field

  • a boolean, for checkboxes

  • a number, for pagination or age form fields

You should not use StateProvider if:

  • your state needs validation logic

  • your state is a complex object (such as a custom class, a list/map, ...)

  • the logic for modifying your state is more advanced than a simple count++.

 

감사합니다!