DEV Community

SangUn
SangUn

Posted on

3 2

Provider Cheatsheet

ChangeNotifierProvider


ChangeNotifierProvider = ChangeNotifier + Provider

Enter fullscreen mode Exit fullscreen mode

Create an instance of ChangeNotifier.
(ChangeNotifier의 어떠한 원본으로 부터의 복제본을 생성한다.)

  1. ChangeNotifier when needed, 필요할때 인스턴스 생성.(lazy),
  2. ChangeNotifier no longer needed, 더이상 필요로 하지 않을때 메모리에서 지워짐.(dispose)

Provide an easy way to access ChangeNotifier for widgets that need it, and rebuilds the UI if necessary
(ChangeNotifier를 필요로 하는 위젯에 ChangeNotifier를 쉽게 적용할 수 있는 수단을 제공하고 필요하면 UI 다시 만든다.)

  1. Constructor를 통해서 인스턴스를 전달할 필요없이 Provider.of를 통해서 ChangeNotifier의 인스턴스에 쉽게 액세스할 수 있다.
  2. Provider.of를 통해서 type T에 인스턴스를 적용할 때 두가지 방법으로 가능
  • Provider.of<T>(context) : 인스턴스를 액세스하고 변화가 있으면 리빌드. -> 데이터와 UI의 동기화
  • Provider.of<T>(context, listen:false) : 인스턴스를 액세스만 하고 변화를 추적하지 않는다. -> Dependency Injection

Provider extendsion methods

//1. value change not listen (변화 감지x)
context.read<T>() == Provider.of<T>(context, listen:false)

//* 2. value change listen(변화 감지O) + ChangeNotifierProvider, StreamPorvider, FutureProvider로 부터의 value를 listen.*/
context.watch<T>() == Provider.of<T>(context)

//* 3. property를 많이 가지고 있는 object의 특정 property만 리슨할 때 사용, 자기가 listen하고 싶은 것만 선별적으로 가능*/
context.select<T, R>(R selector(T value))
ex) 
//개의 이름이 변할때만 리빌드, 퍼포먼스 향상 도움
context.select<Dog, String>((Dog dog) => dog.name)

Enter fullscreen mode Exit fullscreen mode

Sentry mobile image

Improving mobile performance, from slow screens to app start time

Based on our experience working with thousands of mobile developer teams, we developed a mobile monitoring maturity curve.

Read more

Top comments (0)

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay