fix: improve snapshot handling and type safety in MultiStreamBuilder
This commit is contained in:
@@ -228,22 +228,26 @@ class _ExpensesScreenState extends State<ExpensesScreen> with TickerProviderStat
|
||||
],
|
||||
builder: (context, snapshots) {
|
||||
// Handle loading states for all streams
|
||||
// FIX: Add explicit type annotation for the lambda parameter 's'
|
||||
if (snapshots.any((AsyncSnapshot<dynamic> s) => s.connectionState == ConnectionState.waiting && !s.hasData)) {
|
||||
if (snapshots.any((s) => s.connectionState == ConnectionState.waiting && !s.hasData)) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
// Handle error states for all streams
|
||||
// FIX: Add explicit type annotation for the lambda parameter 's'
|
||||
if (snapshots.any((AsyncSnapshot<dynamic> s) => s.hasError)) {
|
||||
// Combine error messages or show a generic one
|
||||
String errors = snapshots.where((s) => s.hasError).map((s) => s.error.toString()).join('\n');
|
||||
return Center(child: Text('Error loading data:\n$errors'));
|
||||
final errorSnapshots = snapshots.where((s) => s.hasError).toList();
|
||||
if (errorSnapshots.isNotEmpty) {
|
||||
return Center(
|
||||
child: Text(
|
||||
'Error loading data:\n${errorSnapshots.map((s) => s.error).join('\n')}',
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Extract data (provide defaults)
|
||||
final expenseCategories = snapshots[0].data as List<Category>? ?? [];
|
||||
final totalIncome = snapshots[1].data as double? ?? 0.0;
|
||||
final totalExpenses = snapshots[2].data as double? ?? 0.0; // Use direct total expenses
|
||||
// Safely extract data with proper type checking
|
||||
try {
|
||||
final expenseCategories = (snapshots[0].data as List<Category>?) ?? [];
|
||||
final totalIncome = (snapshots[1].data as double?) ?? 0.0;
|
||||
final totalExpenses = (snapshots[2].data as double?) ?? 0.0;
|
||||
|
||||
// Main column layout
|
||||
return Column(
|
||||
@@ -591,7 +595,6 @@ extension on AsyncSnapshot<List<AsyncSnapshot>> {
|
||||
// Helper widget to manage multiple streams for the main body
|
||||
class MultiStreamBuilder extends StatelessWidget {
|
||||
final List<Stream<dynamic>> streams;
|
||||
// FIX: Use AsyncWidgetBuilder<List<AsyncSnapshot<dynamic>>> for clarity
|
||||
final AsyncWidgetBuilder<List<AsyncSnapshot<dynamic>>> builder;
|
||||
|
||||
const MultiStreamBuilder({
|
||||
|
||||
Reference in New Issue
Block a user