Harsh Kamani
Harsh Kamani

Reputation: 21

Error : The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type

I'm a beginner Flutter developer, an my code gives me errors.

I'm building a simple meals app and I use toggle button for filtering the meals item but it gives me this error:

Error : The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type

    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      Map<String, bool> _filter = {
        'gluten': false,
        'lactos': false,
        'vegan': false,
        'vegetarian': false,
      };
      List<Meal> _availableMeal = DUMMY_MEALS;
    
      void _setFilters(Map<String, bool> filterData) {
        setState(() {
          _filter = filterData;
          _availableMeal = DUMMY_MEALS.where((meal)**Error Part**{
            
          }).toList();
    
        });
    
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'DeliMeals',
          theme: ThemeData(
              primaryColor: Colors.pink,
              accentColor: Colors.amber,
              canvasColor: Color.fromRGBO(255, 254, 229, 1.0),
              fontFamily: 'Raleway',
              textTheme: ThemeData.light().textTheme.copyWith(
                  body1: TextStyle(color: Color.fromRGBO(20, 51, 51, 1)),
                  body2: TextStyle(color: Color.fromRGBO(20, 51, 51, 1)),
                  title: TextStyle(
                      fontSize: 20,
                      fontFamily: 'RobotoCondensed-italic',
                      fontWeight: FontWeight.bold))),
          //home:CategoriesScreen(),
          initialRoute: '/',
          routes: {
            '/': (ctx) => TabsScreen(),
            CategoryMealsScreen.routeName: (ctx) => CategoryMealsScreen(_availableMeal),
            MealDelailScreen.routeName: (ctx) => MealDelailScreen(),
            FiltersScreen.routeName: (ctx) => FiltersScreen(_setFilters)
          },
          onGenerateRoute: (settings) {
            print(settings.arguments);
            //return MaterialPageRoute(builder: (ctx) => CategoriesScreen());
          },
          onUnknownRoute: (settings) {
            return MaterialPageRoute(
              builder: (ctx) => CategoriesScreen(),
            );
          },
        );
      }
    }

Upvotes: 0

Views: 1541

Answers (2)

T S
T S

Reputation: 21

I ran into the same issue as you did. Here is what I ended up doing.

 void _setFilters(Map<String, bool> filterData) {
    setState(() {
      _filters = filterData;

      _availableMeals = DUMMY_MEALS.where((meal) {
        if (_filters['gluten'] as bool && !meal.isGlutenFree) {
          return false;
        }
        if (_filters['lactose'] as bool && !meal.isLactoseFree) {
          return false;
        }
        if (_filters['vegan'] as bool && !meal.isVegan) {
          return false;
        }
        if (_filters['vegetarian'] as bool && !meal.isVegetarian) {
          return false;
        }

        return true; // <--- I was missing this return

      }).toList();
    });
  }

Upvotes: 1

Omatt
Omatt

Reputation: 10463

The error occurs on the line pointed out in your code _availableMeal = DUMMY_MEALS.where((meal){...}); because the List that you're trying to pass is nullable. What you can do here is add a null-check before assigning the List to _availableMeal. Otherwise, you can make the List itself nullable with List<Meal>? _availableMeal;

Upvotes: 0

Related Questions