• 카테고리

    질문 & 답변
  • 세부 분야

    모바일 앱 개발

  • 해결 여부

    해결됨

Provider 와 StateProvider의 사용에 관해.

23.11.29 08:24 작성 23.11.29 08:24 수정 조회수 361

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

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

0

안녕하세요!

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++.

 

감사합니다!

채널톡 아이콘