Skip to content

Commit 8ec4dba

Browse files
committed
fix(router): resolve type error on multi-select page navigation
Fixes a `_TypeError` that occurred when navigating to the generic `MultiSelectSearchPage`. The error was caused by an incorrect and overly strict type cast of the `itemBuilder` function within the `GoRouter` configuration. The solution is to accept the `itemBuilder` as a generic `Function` in the router and then correctly invoke it within the `MultiSelectSearchPage` constructor. This change respects Dart's type system and allows strongly-typed functions (e.g., `(Country) => String`) to be passed without causing a runtime crash.
1 parent 9797eba commit 8ec4dba

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

lib/router/router.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ import 'package:flutter_news_app_mobile_client_full_source_code/headlines-feed/v
3333
import 'package:flutter_news_app_mobile_client_full_source_code/headlines-feed/view/headlines_feed_page.dart';
3434
import 'package:flutter_news_app_mobile_client_full_source_code/headlines-feed/view/headlines_filter_page.dart';
3535
import 'package:flutter_news_app_mobile_client_full_source_code/headlines-feed/view/manage_saved_filters_page.dart';
36-
import 'package:flutter_news_app_mobile_client_full_source_code/headlines-feed/view/source_filter_page.dart';
3736
import 'package:flutter_news_app_mobile_client_full_source_code/headlines-feed/view/source_list_filter_page.dart';
37+
import 'package:flutter_news_app_mobile_client_full_source_code/headlines-feed/view/source_filter_page.dart';
3838
import 'package:flutter_news_app_mobile_client_full_source_code/headlines-feed/view/topic_filter_page.dart';
3939
import 'package:flutter_news_app_mobile_client_full_source_code/headlines-search/bloc/headlines_search_bloc.dart';
4040
import 'package:flutter_news_app_mobile_client_full_source_code/headlines-search/view/headlines_search_page.dart';
@@ -354,17 +354,19 @@ GoRouter createRouter({
354354
final initialSelectedItems =
355355
extra['initialSelectedItems'] as Set<dynamic>? ?? {};
356356
// The itemBuilder is passed as a function to display the item name.
357+
// We accept it as a generic Function and let the page handle the type,
358+
// avoiding a strict cast here that causes a _TypeError.
357359
final itemBuilder =
358-
extra['itemBuilder'] as String Function(dynamic item)? ??
359-
(item) => item.toString();
360+
extra['itemBuilder'] as Function? ??
361+
(dynamic item) => item.toString();
360362

361363
// Since this is a generic page, we pass the dynamic types directly.
362364
// The calling page is responsible for casting the result.
363365
return MultiSelectSearchPage<dynamic>(
364366
title: title,
365367
allItems: allItems,
366368
initialSelectedItems: initialSelectedItems,
367-
itemBuilder: itemBuilder,
369+
itemBuilder: (dynamic item) => itemBuilder(item) as String,
368370
);
369371
},
370372
),

0 commit comments

Comments
 (0)