1. WidgetsBinding.instance.addPostFrameCallback
ViewModel > Future<void>
ํ์
์ผ๋ก ์ ์ธ๋ async
ํจ์ ํธ์ถํ๊ฒ ๋จ
View > initState()
์์ Provider.notifier
๋ก Firebase ์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋ ค๊ณ ํจ
View > ref.watch(Provider).when(
์ผ๋ก ํ์ธํจ
FlutterError (Tried to modify a provider while the widget tree was building.
If you are encountering this error, chances are you tried to modify a provider
in a widget life-cycle, such as but not limited to:
Life Cycle ๋ฌธ์ ๋ก initState()
์์ ์ฌ์ฉํ ์ ์์
addPostFrameCallback
์ผ๋ก ๋ ์์ด์์ด ๋ชจ๋ ๊ตฌ์ฑ๋ ์ดํ ํธ์ถ
addPostFrameCallback
ํ์ฌ ํ๋ ์์ Rendering ์ด ์๋ฃ๋ ์งํ, ํธ์ถ๋๋ Callback Method
๋ ์ด์์์ ๋ํ ๋ฐ์ดํฐ๊ฐ ๊ตฌ์ฑ๋ ์ดํ์ ์ ๊ทผ ๊ฐ๋ฅ
๋ ์ด์์์ ๋ชจ๋ ๊ตฌ์ฑํ ์ดํ, ํธ์ถํ ํ์๊ฐ ์๋ ๊ฒฝ์ฐ ์ฌ์ฉ
WidgetsBinding .instance.addPostFrameCallback ((_) {
_initTimeTable ();
});
์์ ฏ์ ํฌ๊ธฐ๋ ์์น๋ฅผ ์ป๊ณ ์ถ์ ๋
Rendering ๋ ํ, ํฌ๊ธฐ๋ ์์น๊ฐ ํ์ํ ๊ฒฝ์ฐ
๋์ ์ผ๋ก ์์ ฏ์ ํฌ๊ธฐ๋ฅผ ๊ฒฐ์ ํ๋ ๊ฒฝ์ฐ
GlobalKey _key = GlobalKey ();
@override
void initState () {
super .initState ();
WidgetsBinding .instance.addPostFrameCallback ((_) => _afterLayout ());
}
void _afterLayout () {
final RenderBox renderBox = _key.currentContext? .findRenderObject () as RenderBox ;
final size = renderBox.size;
final position = renderBox.localToGlobal (Offset .zero);
print ("Size: $size , Position: $position " );
}
@override
Widget build (BuildContext context) {
return Container (
key: _key, // ์์ ฏ์ GlobalKey ํ ๋น
// ์์ ฏ ๊ตฌ์ฑ
);
}
์ด๊ธฐ ์ ๋๋ฉ์ด์
์คํ
์ฑ์ด ์์ํ ๋, ํน์ ์ ๋๋ฉ์ด์
์ ๋ณด์ฌ์ฃผ๊ณ ์ถ์ ๊ฒฝ์ฐ
@override
void initState () {
super .initState ();
WidgetsBinding .instance.addPostFrameCallback ((_) => _startAnimation ());
}
void _startAnimation () {
// ์ ๋๋ฉ์ด์
์ปจํธ๋กค๋ฌ ์ด๊ธฐํ ๋ฐ ์์ ๋ก์ง
}
๋ฐ์ดํฐ ๋ก๋ฉ ๋ฐ UI ์
๋ฐ์ดํธ
์์ ฏ์ด ํ๋ฉด์ ๊ทธ๋ ค์ง๋ ์์ ์, ์ธ๋ถ API๋ก ๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์์ผ ํ๋ ๊ฒฝ์ฐ
@override
void initState () {
super .initState ();
WidgetsBinding .instance.addPostFrameCallback ((_) => _loadData ());
}
void _loadData () async {
final data = await fetchDataFromNetwork (); // ๋คํธ์ํฌ์์ ๋ฐ์ดํฐ๋ฅผ ๋น๋๊ธฐ์ ์ผ๋ก ๋ก๋
setState (() {
// ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ์ฌ ์ํ ์
๋ฐ์ดํธ
});
}