Edit: Minimal reproducible example. Please have a look at the refresh-Method in main.dart
.
I'm using a FutureBuilder
to display the result of a network request. I call my fetch method in initstate
(like suggested here):
Future<List<CheckIn>> futureCheckIn;
@override
void initState() {
super.initState();
_eventArg = Provider.of<EventArg>(context, listen: false);
_helper = ApiHelper(context);
futureCheckIn = _helper.fetchCheckIns(_eventArg.getEvent().id.toString());
}
I now want to use a RefreshIndicator
to allow the user to refresh the screen.
This works great until my network request ends with an exception (e.g. 404). Normally the exception get caught by the FutureBuilder
but after excecution the request in the RefreshIndicator
the exception is unhandled...
Here my code:
RefreshIndicator buildResult(List<CheckIn> checkIns, BuildContext context) {
return RefreshIndicator(
key: _refreshKey,
onRefresh: () async {
setState(() {});
futureCheckIn =
_helper.fetchCheckIns(_eventArg.getEvent().id.toString()); // this could end with an exception
},
child: Container(
height: MediaQuery.of(context).size.height,
child: SingleChildScrollView(
physics: AlwaysScrollableScrollPhysics(),
scrollDirection: Axis.vertical,
child: Column(
children: [
Container(
margin: EdgeInsets.all(10),
child: Text(
constants.CHECK_IN_SCREEN_DESCRIPTION,
style: TextStyle(fontSize: 16),
textAlign: TextAlign.justify,
),
),
ListView(
physics: NeverScrollableScrollPhysics(),
padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
scrollDirection: Axis.vertical,
shrinkWrap: true,
children: checkIns.map((el) {
return Card(
elevation: 4,
child: ListTile(
title: Text(el.name),
onTap: () async {
...
},
trailing: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.arrow_forward_ios,
color: Colors.black,
)
],
),
),
);
}).toList(),
)
],
),
),
),
);
}
How can I use the RefreshIndicator
properly with FutureBuilder
?
Edit @Shri Hari: This exception is thrown
class AppException implements Exception {
final _message;
final _prefix;
AppException([this._message, this._prefix]);
String toString() {
return "$_prefix$_message";
}
}
class NotFoundException extends AppException {
NotFoundException([String message]) : super(message, "Data not found: ");
}
Edit @Mhark Batoctoy: Yes it is part of the FutureBuilder
:
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: futureCheckIn,
builder: (context, snapshot) {
if (snapshot.hasData) {
List<CheckIn> checkIns = snapshot.data;
return buildResult(checkIns, context); // RefreshIndicator is nested in here
} else if (snapshot.hasError) {
// Error handling
...
}
// Spinner während der Datenabfrage
return Center(
child: CircularProgressIndicator(),
);
},
);
Unfortunatelly changing the onRefresh
-Method from
onRefresh: () async {
setState(() {});
futureCheckIn =
_helper.fetchCheckIns(_eventArg.getEvent().id.toString()); // this could end with an exception
},
to
onRefresh: () async {
setState(() {});
return futureCheckIn =
_helper.fetchCheckIns(_eventArg.getEvent().id.toString()); // this could end with an exception
},
or
onRefresh: () async {
setState(() {});
return _helper.fetchCheckIns(_eventArg.getEvent().id.toString()); // this could end with an exception
},
does not change the error...