This app shows a spinner for 3 seconds to simulate loading. Then it shows a success message. You can change hasError to true to see the error message instead.
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool isLoading = true;
bool hasError = false;
@override
void initState() {
super.initState();
// Simulate loading data
Future.delayed(const Duration(seconds: 3), () {
setState(() {
isLoading = false;
hasError = false; // Change to true to test error state
});
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Loading and Error States')),
body: Center(
child: isLoading
? const CircularProgressIndicator()
: hasError
? const Text('Error loading data', style: TextStyle(color: Colors.red, fontSize: 18))
: const Text('Data loaded successfully!', style: TextStyle(fontSize: 18)),
),
),
);
}
}