isLoading vsValueNotifier
Using ValueNotifier
class HomePage extends StatefulWidget {
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final ValueNotifier<bool> isLoading = ValueNotifier(false);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Home Page")),
body: ValueListenableBuilder<bool>(
valueListenable: isLoading,
builder: (context, loading, child) {
if (loading) {
return const Center(child: CircularProgressIndicator());
}
// Other content...
},
),
);
}
Future<void> _handleFunction(String id) async {
isLoading.value = true;
try {
await databaseService.addToDatabase(widget.agentUid, routeId);
} catch (error) {
_alertService.showToast(text: 'Failed to add route.', icon: Icons.error);
} finally {
isLoading.value = false;
}
}
}
Using isLoading
class HomePage extends StatefulWidget {
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
bool isLoading = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Home Page")),
body: isLoading
? const Center(child: CircularProgressIndicator())
: // Other content...
Center(child: Text("Content goes here")),
);
}
Future<void> _handleFunction(String id) async {
setState(() {
isLoading = true;
});
try {
await databaseService.addToDatabase(widget.agentUid, routeId);
} catch (error) {
_alertService.showToast(text: 'Failed to add route.', icon: Icons.error);
} finally {
setState(() {
isLoading = false;
});
}
}
}