class PostDetailBody extends ConsumerWidget {
int postId;
PostDetailBody(this.postId);
@override
Widget build(BuildContext context, WidgetRef ref) {
// FutuerProvider, StreamProvider, StateProvider, NotifierProvider
// 프로바이더 값 넘기기
PostDetailModel? model = ref.watch(postDetailProvider(postId));
if (model == null) {
return Center(child: CircularProgressIndicator());
} else {
return Padding(
padding: const EdgeInsets.all(16.0),
child: ListView(
children: [
PostDetailTitle("${model.post.title}"),
const SizedBox(height: largeGap),
PostDetailProfile(model.post),
PostDetailButtons(model.post),
const Divider(),
const SizedBox(height: largeGap),
PostDetailContent("${model.post.content}"),
],
),
);
}
}
}
provider에 id값을 넘겨줘 봅시다.
ViewModel에서
final postDetailProvider =
NotifierProvider.family<PostDetailVM, PostDetailModel?, int>(() {
return PostDetailVM();
});
NotifierProvider에 .family를 사용한 뒤, 제너릭의 세번째 위치에 넘겨받은 타입을 명시해 줍니다.
※ 객체도 가능하다..
이후 Extends Notifier<
PostDetailModel?
>를 FamilyNotifer<
PostDetailModel?
, int> 로 변경해줍니다.아래의 build( )에도 id 를 넣어줍니다. ⇒ build(id)

init 메서드도 초기에 id값을 받아낼 수 있게 되었습니다.

페이지에 들어올 때 init이 실행되면서 id를 받아내어
통신을 할 때, id값마다 일치하는 글을 가져올 수 있게 됩니다.
Share article