i have a list view and i want to get a finishedDate from firebase for every listTile if condition is serving. but i got an error future is not subtype of DateTime
what did i do is:
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: widget.servineraProviders!.length,
itemBuilder: (context, int currentIndex){
DateTime? finishedDate;
String state = widget.servineraProviders![currentIndex].providerState;
if(state == "Serving")
finishedDate = getFinishedDate(widget.servineraProviders![currentIndex].providerID);
return Stack(
children: <Widget>[
Container(
width:double.infinity,
color: Colors.grey[50],
child: state == "Serving" ? Text(finishedDate.toString) : Text("provider is available")
)
],
);
},
);
}
the finishedDate function is as follows
getFinishedDate(providerID)async {
DateTime dt = await DatabaseService().getFinishedTimeOfService(providerID);
return dt;
}
then the getFinishedTiumeofService:
Future<DateTime> getFinishedTimeOfService(providerID) async {
DateTime finishedDate = DateTime.now();
try{
await ordersCollection
.where('orderStatus', isEqualTo: 'inprogress')
.where('providerID', isEqualTo: providerID)
.get()
.then((querySnapshot){
var doc = querySnapshot.docs[0].data() as Map<String, dynamic>;
DateTime timeArrived = doc['timeArrived'].toDate();//DateFormat("hh:mm").parse('${data[0].timeArrived}');
List<String> parts = doc['estimatedTime'].split(':');
int hours = 0, minutes = 0;
if(parts.length > 2) hours = int.parse(parts[parts.length - 3]);
if(parts.length > 1) minutes = int.parse(parts[parts.length -2]);
Duration d = Duration(hours: hours, minutes: minutes);
finishedDate = timeArrived.add(d);
});
return finishedDate;
}catch(e){
print(e.toString());
return DateTime.now();
}
}
CodePudding user response:
You can wrap your Stack
widget with Future Builder
CodePudding user response:
wrap your widget with Future Builder
CodePudding user response:
Just wrap your Stack widget with FutureBuilder widget and pass the getFinishedDate method to future property of FutureBuilder
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: widget.servineraProviders!.length,
itemBuilder: (context, int currentIndex){
DateTime? finishedDate;
String state = widget.servineraProviders![currentIndex].providerState;
return FutureBuilder(
future: getFinishedDate(widget.servineraProviders![currentIndex].providerID),
builder: (context, snapshot) {
if (snapshot.hasData && !snapshot.hasError) {
return Stack(
children: <Widget>[
Container(
width: double.infinity,
color: Colors.grey[50],
child: Text(snapshot.data.toString)
)
],
);
} else if (snapshot.hasError){
print(snapshot.error.toString());
return Text(snapshot.error.toString());
} else {
return CircularProgressIndicator();
}
}
);
},
);
}