顫振錯誤:位置引數過多:預期為 0,但在流構建器中發現 1 我已經看到很多文章對此進行了更新,但我無法獲得正確的解決方案,所以請幫助我如何解決它。
代碼:
StreamBuilder(
stream: _products.snapshots(), //build connection
builder: (context, AsyncSnapshot<QuerySnapshot> streamSnapshot) {
if (streamSnapshot.hasData) {
return ListView.builder(
itemCount: streamSnapshot.data!.docs.length, //number of rows
itemBuilder: (context, index) {
final DocumentSnapshot documentSnapshot =
streamSnapshot.data!.docs[index];
return Card(
margin: const EdgeInsets.all(10),
child: ListTile(
title: Text(documentSnapshot['name']),
subtitle: Text(documentSnapshot['price'].toString()),
),
);
},
);
}
return const Center(
child: CircularProgressIndicator(),
);
},
)
在此處輸入影像描述
uj5u.com熱心網友回復:
您的流構建器看起來不在小部件樹中。請試試這個
Scaffold(
floatingActionButton : FloatingActionButton(),
body : SizedBox(
width: double.infinity,
child: Center(
child: StreamBuilder(
stream: _products.snapshots(),
initialData: 0,
builder: (
BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot,
) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Container( );
} else if (snapshot.connectionState == ConnectionState.active
|| snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
return const Text('Error');
} else if (snapshot.hasData) {
return Text(
snapshot.data.toString(),
);
} else {
return const Text('Empty data');
}
} else {
return Text('State: ${snapshot.connectionState}');
}
),
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/497921.html
上一篇:使用異步功能更改通知提供程式
