Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

removeWhere() method does not remove the data

I am building a food recipe app where user can browse various recipes.
The functionality is that, when user hit delete button, the item will not be shown in listing. I navigated the the mealId to the previous screen, i.e. Listing screen through

floatingActionButton: FloatingActionButton(
    onPressed: () {
      Navigator.of(context).pop(mealId);
    },
    child: const Icon(Icons.delete),
  ),

I receive the pop() value in backward widget like:

void selectMeal(BuildContext context) {
    Navigator.of(context)
        .pushNamed(MealsDetailsScreen.routeName, arguments: id)
        .then((result) {
      if (result != null) {
        removeItem(result);
        print(result); // it prints the expected id
      }
    });
  }

And in the code attached fully, I wanted to remove the item details via mealId

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

void _removeMeal(String mealId) {
    setState(() {
      print("$mealId from didChangedDependancies"); //it also prints the expected id
      displayedMeals.removeWhere((meal) => meal.id == mealId);
    });
  }

The code where I set the function to remove:

import 'package:flutter/material.dart';

import '../models/meals.dart';
import '../models/dummy_data.dart';
import '../widgets/meal_item.dart';

class CategoryMealaScreen extends StatefulWidget {
  static const routeName = '/category-meals';
  @override
  State<CategoryMealaScreen> createState() => _CategoryMealaScreenState();
}

class _CategoryMealaScreenState extends State<CategoryMealaScreen> {
  late String categoryTitle;
  late List<Meal> displayedMeals;
  var _loadedInitData = false;

  @override
  void initState() {
    super.initState();
  }

  @override
  void didChangeDependencies() {
    if (!_loadedInitData) {
      final routeArgs =
          ModalRoute.of(context)!.settings.arguments as Map<String, String>;

      categoryTitle = routeArgs['title'].toString();
      final categoryId = routeArgs['id'];
      displayedMeals = dummyMeals.where((meal) {
        return meal.categories.contains(categoryId);
      }).toList();
      _loadedInitData = true;
    }
    super.didChangeDependencies();
  }

  void _removeMeal(String mealId) {
    setState(() {
      print("$mealId from didChangedDependancies");
      displayedMeals.removeWhere((meal) => meal.id == mealId);
    });
  }

  @override
  Widget build(BuildContext context) {
    final routeArgs = // received data from widget CategoryItems()
        ModalRoute.of(context)!.settings.arguments as Map<String, String>;

final categoryTitle = routeArgs['title'];
final categoryId = routeArgs['id'];
final displayedMeals = dummyMeals.where((meal) {
  return meal.categories.contains(categoryId);
}).toList();

return Scaffold(
  appBar: AppBar(
    title: Text(categoryTitle.toString()),
  ),
  body: ListView.builder(
      itemCount: displayedMeals.length,
      itemBuilder: (ctx, index) {
        return MealItem(
          id: displayedMeals[index].id,
          title: displayedMeals[index].title,
          imageUrl: displayedMeals[index].imageUrl,
          complexity: displayedMeals[index].complexity,
          affordability: displayedMeals[index].affordability,
          duration: displayedMeals[index].duration,
          removeItem: _removeMeal,
        );
      }),
    );
  }
}

No error shows on console.
I’ll be vary happy if you guys help me out! Thanks a lot😎

>Solution :

Remove final displayedMeals inside your build method.

Use the displayedMeals variable outside your build method instead.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading