인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

SY. Jeoung's profile image
SY. Jeoung

asked

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

Using a Provider within a Provider

Provider 와 StateProvider의 사용에 관해.

Resolved

Written on

·

462

·

Edited

0

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

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

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

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

 

flutter하이브리드-앱

Answer 2

0

SY. Jeoung님의 프로필 이미지
SY. Jeoung
Questioner

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

0

codefactory님의 프로필 이미지
codefactory
Instructor

안녕하세요!

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

 

감사합니다!

SY. Jeoung's profile image
SY. Jeoung

asked

Ask a question