1. ๋ชจ๋ธ ์์ฑ
QuerySnapshot ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด์ฌ๋, ์ํ๋ ์ค๋ธ์ ํธ ํํ๋ก ๊ฐ์ ธ์ค๊ฒ ํจ. (Category ์ค๋ธ์ ํธ ํ์ ์ผ๋ก ํ์ด์ด์คํ ์ด์ data writing๋ ์ํ)
class HomeModel {
final Stream<QuerySnapshot<Category>> categoriesStream = FirebaseFirestore.instance
.collection(Constants().categories)
.withConverter<Category>(
fromFirestore: (snapshot, _) => Category.fromJson(snapshot.data()!),
toFirestore: (category, _) => category.toJson(),
)
.snapshots();
}
2. ์ถ๋ ฅ์ ์ํ๋ ์์ ฏ์์ StreamBuilder๋ก ๊ฐ์ธ๊ธฐ
์๋๋ Wrap ์์ ฏ์ด์์.
3. snapshot ๋ฐ์ดํฐ์ ๋ํด ์์ธ์ฒ๋ฆฌ ์ถ๊ฐํด์ฃผ๊ณ , ์๋ text ์ถ๋ ฅํ๋ ๋ถ๋ถ์์ ์ค์ ๋ฐ์ดํฐ ๋ฝ์๋ด์ ์ถ๋ ฅ
StreamBuilder<QuerySnapshot<Category>>(
stream: model.categoriesStream,
builder: (context, snapshot) {
if (snapshot.hasError) {
return const Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
}
List<Category> categories = snapshot.data!.docs.map((e) => e.data()).toList();
return Wrap(
direction: Axis.horizontal, //๋์ด ๋ฐฉํฅ
alignment: WrapAlignment.start, //์ ๋ ฌ๋ฐฉ์
spacing: 5, //์ข์ฐ๊ฐ๊ฒฉ
runSpacing: 5, //์ํ๊ฐ๊ฒฉ
children: <Container>[
for(var i = 0; i<categories.length; ++i)
...[
Container(
padding: const EdgeInsets.all(6),
decoration: BoxDecoration(
color: const Color(0xffdddddd),
borderRadius: BorderRadius.circular(6),
),
child: Text('#${categories[i].name}'),
),
],
],
);
}
),
๋๊ธ