It should be a simple thing, but I've been in this for days!
///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
],
),
),
),
)
);
},
),
Now, I want to do a operation with the data, like this:
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"),
And the error is:
type 'int' is not a subtype of type 'Widget'
What's wrong where?
CodePudding user response:
You cant declare var inside children list what you have to do is
StreamBuilder(
stream: farmacos.orderBy("nome", descending: false).snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> streamSnapshot) {
if (!streamSnapshot.hasData) {
return const Text("No data...");
}
_dose = streamSnapshot.data!.docs[index]["dose_ml_Kg"]*20,// //thats what you have to do
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: [
Text(streamSnapshot.data!.docs[index]["nome"].toString()),
Text(streamSnapshot.data!.docs[index]["dose_ml_Kg"].toString(),),
Text(streamSnapshot.data!.docs[index]["dose_max"].toString(),),
Text("$_dose"),
],
),
),
),
)
);
},
),