這是我的作業代碼:
final Stream<QuerySnapshot> products = FirebaseFirestore.instance
.collection('products')
.doc('men')
.collection('tshirts')
.snapshots();
這是我想要但不起作用的代碼:
String val = 'tshirts';
final Stream<QuerySnapshot> products = FirebaseFirestore.instance
.collection('products')
.doc('men')
.collection(val)
.snapshots();
錯誤: 無法在初始化程式中訪問實體成員“val”。嘗試用不同的運算式替換對實體成員的參考
我在構建小部件中使用的波紋管代碼來顯示資料庫查詢結果并且作業正常:
GridView.count(
physics: const BouncingScrollPhysics(),
crossAxisCount: 2,
childAspectRatio: 0.5,
mainAxisSpacing: 30,
crossAxisSpacing: 10,
children: snapshot.data!.docs.map((DocumentSnapshot document) {
Map<String, dynamic> data =
document.data()! as Map<String, dynamic>;
return Visibility(
child: GestureDetector(
onTap: () {
Navigator.push(
context,
PageTransition(
type: PageTransitionType.bottomToTop,
child: const ProductView(productId: '3453245'),
));
},
child: Stack(
children: [
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage('${data['img']}'),
fit: BoxFit.cover)),
),
Align(
alignment: Alignment.bottomLeft,
child: SizedBox(
height: 70,
width: double.infinity,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
margin: const EdgeInsets.only(
right: 18, top: 15, left: 5),
child: Text(
data['name'].toUpperCase(),
textAlign: TextAlign.left,
style: GoogleFonts.oswald(
color: Colors.black,
fontSize: 15,
),
overflow: TextOverflow.ellipsis,
maxLines: 1,
softWrap: false,
),
),
Container(
margin: const EdgeInsets.only(left: 5),
child: Text(
'\u{20B9} ${data['price']}',
textAlign: TextAlign.left,
style: const TextStyle(
color: Colors.black,
fontSize: 14,
),
overflow: TextOverflow.ellipsis,
maxLines: 1,
),
)
],
),
).frosted(
blur: 7,
),
),
],
)),
visible: true,
);
}).toList(),
)
uj5u.com熱心網友回復:
您val甚至在初始化所使用的類之前就嘗試使用。您只能在方法或建構式中訪問它,例如:
@override
void initState() {
super.initState();
final Stream<QuerySnapshot> products = FirebaseFirestore.instance.collection('products').doc('men').collection(val).snapshots();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/390014.html
標籤:火力基地 扑 镖 谷歌云firestore
