使用 streambuilder 的值进行操作
Operation with values of a streambuilder
这应该是一件简单的事情,但我已经搞了好几天了!
///StreamBuilder
StreamBuilder(
stream: farmacos.orderBy("nome", descending: false).snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> streamSnapshot) {
if (!streamSnapshot.hasData) {
return const Text("No data...");
}
return ListView.builder(
itemCount: streamSnapshot.data!.docs.length,
itemBuilder: (context, index) => GestureDetector(
onTap: () {},
child: Card(
color: Colors.amberAccent[100],
child: Padding(
padding: EdgeInsets.fromLTRB(_margem * .1,
_margem * .1, _margem * .1, _margem * .1),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
///Get data from the stream
Text(streamSnapshot.data!.docs[index]["nome"].toString()), //Ex: Paracetamol
Text(streamSnapshot.data!.docs[index]["dose_ml_Kg"].toString(),), //Ex: 40
Text(streamSnapshot.data!.docs[index]["dose_max"].toString(),), //Ex: 1000
],
),
),
),
)
);
},
),
Here the result
现在,我想对数据做一个操作,像这样:
Text(streamSnapshot.data!.docs[index]["nome"].toString()),
Text(streamSnapshot.data!.docs[index]["dose_ml_Kg"].toString(),),
Text(streamSnapshot.data!.docs[index]["dose_max"].toString(),),
_dose = streamSnapshot.data!.docs[index]["dose_ml_Kg"]*20,
Text("$_dose"),
错误是:
type 'int' is not a subtype of type 'Widget'
哪里出了问题?
您不能在子列表中声明 var
你要做的是
streamSnapshot.data!.docs.length,
itemBuilder: (context, index){
_dose = streamSnapshot.data!.docs[index]["dose_ml_Kg"]*20;
return GestureDetector(
onTap: () {}
,
child: Card(
color: Colors.amberAccent[100],
child: Padding(
padding: EdgeInsets.fromLTRB(_margem * .1,
_margem * .1, _margem * .1, _margem * .1),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
///Get data from the stream
Text(streamSnapshot.data!.docs[index]["nome"].toString()), //Ex: Paracetamol
Text(streamSnapshot.data!.docs[index]["dose_ml_Kg"].toString(),), //Ex: 40
Text(streamSnapshot.data!.docs[index]["dose_max"].toString(),),
Text("$_dose"),//Ex: 1000
//Text("$_dose"),
],
),
),
),
);
},
);
},
),
这应该是一件简单的事情,但我已经搞了好几天了!
///StreamBuilder
StreamBuilder(
stream: farmacos.orderBy("nome", descending: false).snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> streamSnapshot) {
if (!streamSnapshot.hasData) {
return const Text("No data...");
}
return ListView.builder(
itemCount: streamSnapshot.data!.docs.length,
itemBuilder: (context, index) => GestureDetector(
onTap: () {},
child: Card(
color: Colors.amberAccent[100],
child: Padding(
padding: EdgeInsets.fromLTRB(_margem * .1,
_margem * .1, _margem * .1, _margem * .1),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
///Get data from the stream
Text(streamSnapshot.data!.docs[index]["nome"].toString()), //Ex: Paracetamol
Text(streamSnapshot.data!.docs[index]["dose_ml_Kg"].toString(),), //Ex: 40
Text(streamSnapshot.data!.docs[index]["dose_max"].toString(),), //Ex: 1000
],
),
),
),
)
);
},
),
Here the result
现在,我想对数据做一个操作,像这样:
Text(streamSnapshot.data!.docs[index]["nome"].toString()),
Text(streamSnapshot.data!.docs[index]["dose_ml_Kg"].toString(),),
Text(streamSnapshot.data!.docs[index]["dose_max"].toString(),),
_dose = streamSnapshot.data!.docs[index]["dose_ml_Kg"]*20,
Text("$_dose"),
错误是:
type 'int' is not a subtype of type 'Widget'
哪里出了问题?
您不能在子列表中声明 var 你要做的是
streamSnapshot.data!.docs.length,
itemBuilder: (context, index){
_dose = streamSnapshot.data!.docs[index]["dose_ml_Kg"]*20;
return GestureDetector(
onTap: () {}
,
child: Card(
color: Colors.amberAccent[100],
child: Padding(
padding: EdgeInsets.fromLTRB(_margem * .1,
_margem * .1, _margem * .1, _margem * .1),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
///Get data from the stream
Text(streamSnapshot.data!.docs[index]["nome"].toString()), //Ex: Paracetamol
Text(streamSnapshot.data!.docs[index]["dose_ml_Kg"].toString(),), //Ex: 40
Text(streamSnapshot.data!.docs[index]["dose_max"].toString(),),
Text("$_dose"),//Ex: 1000
//Text("$_dose"),
],
),
),
),
);
},
);
},
),