0
 class _MyAppState extends State<MyApp> {
          Map<String, bool> _filters = {
            'gluten': false,
            'lactose': false,
            'vegan': false,
            'vegetarian': false,
          };
        
      List<Meal> _availableMeals = DUMMY_MEALS;
    
      void _setFilters(Map<String, bool> filterData) {
        setState(() {
      _filters = filterData;
      _availableMeals = DUMMY_MEALS.where(
        (meal) {
          if (_filters['gluten'] && !meal.isGlutenFree) {
            return false;
          }
 
          return true;
        },
      ).toList();
    });
  }

  final routes = <String, WidgetBuilder>{
    TabsScreen.tag: (context) => TabsScreen(),
    HomePage.tag: (context) => HomePage(),
    FilterPage.tag: (context) => FilterPage(_setFilters),
    CategoryMealScreen.tag: (context) => CategoryMealScreen(_availableMeals),
    MealDetailScreen.tag: (context) => MealDetailScreen(),
  };

  @override
  Widget build(BuildContext context) {
    const String _category = 'Categories';

    return MaterialApp(
      title: _category,
      home: TabsScreen(),
      debugShowCheckedModeBanner: false,
      routes: routes,
      onUnknownRoute: (settings) {
        return MaterialPageRoute(
          builder: (ctx) => HomePage(),
        );
      },
    );
  }
}

I got this error when I am trying to pass the setFilter function to the FilterPage and an error with" The instance member '_setFilters' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expression. can anyone help me fix this

class FilterPage extends StatefulWidget {
  static String tag = 'filter-tag';

  final Function saveFilters;

  const FilterPage(this.saveFilters);

  @override
  State<FilterPage> createState() => _FilterPageState();
}
  • Does this answer your question? [Flutter Instance member ‘{0}’ can’t be accessed using static access](https://stackoverflow.com/questions/60999971/flutter-instance-member-0-can-t-be-accessed-using-static-access) – Tirth Patel Jul 22 '21 at 10:29

1 Answers1

0

I was facing the same issue. I resolved this by passing following instruction inside a function.

const FilterPage(this.saveFilters);
Milvintsiss
  • 1,420
  • 1
  • 18
  • 34
San
  • 64
  • 2