title: Working with flutter and just encountered the following error that says invalid range value
body: <p><a href="https://i.stack.imgur.com/wT1NT.png" rel="nofollow noreferrer">error I get</a>//below is the main file of my flutter project . in the main dart I am passing favourite_meal to //the favourite_screen, However it gives me an error rangeerror (index) invalid value valid value //range is empty 0 .. error it shows comes from route section where i register my routes when i //pass favourites_meal to  '/': (ctx) =&gt; tabScreen(favourite_meals),</p>
  <pre><code>import 'package:meals_app/dummy_data.dart';
  import 'package:meals_app/filters.dart';
  import 'package:meals_app/meal_screen.dart';
  import 'package:meals_app/models/meals.dart';
  import 'package:meals_app/tabs_screen.dart';
  import 'tabs_screen.dart';
  import '/categories_meals_screen.dart';
  import '/categories_screen.dart';
  import 'package:flutter/cupertino.dart';
  import 'package:flutter/material.dart';
  
  void main() {
    runApp(MyApp());
  }
  
  class MyApp extends StatefulWidget {
    // This widget is the root of your application.
    @override
    State&lt;MyApp&gt; createState() =&gt; _MyAppState();
  }
  
  class _MyAppState extends State&lt;MyApp&gt; {
    Map&lt;String, bool&gt; filters = {
      'gluten': false,
      'lactose': false,
      'vegetarian': false,
      'vegan': false
    };
  
    List&lt;Meal&gt; available_meals = DUMMY_MEALS;
  
    List&lt;Meal&gt; favourite_meals = [];
  
    void setfilters(Map&lt;String, bool&gt; filterData) {
      setState(() {
        filters = filterData;
        available_meals = DUMMY_MEALS.where((meal) {
          if (filters['gluten'] == true &amp;&amp; !meal.isGlutenFree) {
            return false;
          }
          if (filters['lactose'] == true &amp;&amp; !meal.isLactoseFree) {
            return false;
          }
  
          if (filters['vegetarian'] == true &amp;&amp; !meal.isVegetarian) {
            return false;
          }
          if (filters['vegan'] == true &amp;&amp; !meal.isVegan) {
            return false;
          }
          return true;
        }).toList();
      });
    }
  
    void togglefavourites(String mealId) {
      final existing_index =
          favourite_meals.indexWhere((meal) =&gt; meal.id == mealId);
  
      if (existing_index &gt;= 0) {
        setState(() {
          favourite_meals
              .removeAt(existing_index); //if returns -1 no index exists
        });
      } else {
        setState(() {
          favourite_meals.add(
            DUMMY_MEALS.firstWhere((meal) =&gt; meal.id == mealId),
          );
        }); //look in dummy meals and return that index -add to favourite meals
      }
    }
  
    bool ismealFav(String id) {
      return favourite_meals.any((meal) =&gt; meal.id == id);
    }
  
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        title: 'Meals App',
        theme: ThemeData(
          primarySwatch: Colors.pink,
          accentColor: Colors.amber,
          canvasColor: Color.fromRGBO(255, 254, 229, 1),
          fontFamily: 'Raleway',
          textTheme: ThemeData.light().textTheme.copyWith(
              bodyText1: TextStyle(
                color: Color.fromRGBO(20, 51, 1, 1),
              ),
              bodyText2: TextStyle(
                color: Color.fromRGBO(20, 51, 1, 1),
              ),
              subtitle1: TextStyle(
                fontSize: 20,
                fontFamily: 'Raleway',
                fontWeight: FontWeight.bold,
              )),
        ),
        initialRoute: '/',
        // home: CategoriesScreen(),
        routes: {
          '/': (ctx) =&gt; tabScreen(favourite_meals),
          '/category-meals': (ctx) =&gt; CategoriesMealScreen(available_meals),
          mealdetailScreen.routeName: (ctx) =&gt;
              mealdetailScreen(togglefavourites, ismealFav),
          filter_screen.route_name: (ctx) =&gt; filter_screen(setfilters, filters),
        },
  //   onGenerateRoute:(settings){
  //     print(settings.arguments);
  //     return MaterialPageRoute(builder: (ctx)=&gt;CategoriesScreen(),);
  // } ,
        onUnknownRoute: (settings) {
          return MaterialPageRoute(
            builder: (ctx) =&gt; CategoriesScreen(),
          );
        },
      );
    }
  }
  
          
  </code></pre>
  <p>// this is the screen where I am passing down the favourites meal list from main file.I have //tried flutter clean but still no luck . the listview builder isgetting the itemcount.</p>
  <pre><code>import 'package:flutter/material.dart';
  import 'package:meals_app/meals_item.dart';
  import 'package:meals_app/models/meals.dart';
  
  class FavouritesScreen extends StatelessWidget {
    final List&lt;Meal&gt; thefavourites;
    FavouritesScreen(this.thefavourites);
  
    @override
    Widget build(BuildContext context) {
      if (thefavourites.isEmpty == true) {
        return Container(
          child: Text('No Favourites'),
        );
      } else {
        return ListView.builder(
          itemBuilder: (ctx, index) {
            return MealItem(
              id: thefavourites[index].id,
              title: thefavourites[index].title,
              Duration: thefavourites[index].duration,
              imgurl: thefavourites[index].imageUrl,
              affordability: thefavourites[index].affordability,
              complexity: thefavourites[index].complexity,
            );
          },
          itemCount: thefavourites.length,
        );
      }
    }
  }
  </code></pre>