简体   繁体   English

如何使用 Flutter 提供程序 package 从列表中添加和删除项目?

[英]How add and remove items from a List using Flutter provider package?

Hello I am trying to add Items to a List _bookMarkedItems .您好我正在尝试将项目添加到列表_bookMarkedItems

class Item with ChangeNotifer{
  String id;
  String title;
  bool isBookMarked;
  Item(this.id,this.title,this.isBookMarked);
  void toggleBookMark(){
    isBookMarked=!isBookMarked;
    notifyListneres();
  }
}

Items Class项目 Class

class Items with ChangeNotifier{
 //recommendedBooks
  List<Item> _recommendedItems=[
  Item('1','Groccery'),
  Item('1','Groccery'),
 ];
  
  //Recommended
  List<Item> _discoverNew=[
  Item('1','Groccery'),
  Item('1','Groccery'),
 ];
 
 List<Item> _bookMarkedItems=[];
//getters
  List<Item> get recommendedItems{
  return [..._recommendedItems];
  }
  List<Item> get discoverNew{
  return [..._discoverNew];
  }
  
  List<Item> get bookMarkedItems{
  _recommendedItems.forEach((item){
    if(item.isBookMarked)
      _bookMarkedItems.add(item);
  });
      _discoverNew.forEach((item){
    if(item.isBookMarked)
      _bookMarkedItems.add(item);
  });
    return [..._bookMarkedItems];
  }
}

When I tap on any Item from _discoverNew or _recommendedItems in the homescreen.dart.当我点击主屏幕中的_discoverNew_recommendedItems中的任何项目时。dart。 //It builds the bookMarkedItems

It ADDS them to the _bookMarkedItems .它将它们添加到_bookMarkedItems中。

But when I go back homescreen and tap on the item again.但是当我 go 返回主屏幕并再次点击该项目时。

//It does not builds the bookMarkedItems and It does NOT update the and not REMOVE item from List _bookMarkedItems . //It does not builds the bookMarkedItems并且它不更新并且不从 List _bookMarkedItems中删除项目。 (Here is what I am not understanding). (这是我不明白的)。 I can add more items from the home page but cannot remove them.我可以从主页添加更多项目,但不能删除它们。 In MyApp above myhomepage.dart ChangeNotifierProvider(create:(_)=>Items(),.. myitems.dart在 MyApp 上面 myhomepage.dart ChangeNotifierProvider(create:(_)=>Items(),.. myitems.dart

  Widget build(BuildContext context) {
    var savedItems= Provider.of<Items>(context).bookMarkedItems;
    print("**************Building savedItem List");
    return Scaffold(
      body: Container(```


  [1]: https://i.stack.imgur.com/eIS8X.png

make the list of items null within bookMarkedItem function, like this: _bookMarkedItems = [];在 bookMarkedItem function 中列出项目 null,如下所示:_bookMarkedItems = [];

try to change isBookMarked = !isBookMarked尝试更改 isBookMarked = !isBookMarked

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试使用提供程序从列表视图中删除项目时,它从 flutter 的列表中删除了最后一个元素 - When I'm trying to remove items from the listview using provider it removed last element from the list in flutter 使用提供程序 flutter 从列表中添加和删除项目 - Adding and Removing items from a list using provider flutter 如何使用 Flutter 中的 Provider 包更改图像 - How to change images using the Provider Package in Flutter When I am using the provider package in Flutter to load data from an API into a list it repeatedly calls the API, how do I fix it? - When I am using the provider package in Flutter to load data from an API into a list it repeatedly calls the API, how do I fix it? Dart // Flutter:如何根据条目的内容从列表中删除项目 - Dart // Flutter: How to remove Items from List depending on content of entry Flutter - 使用 ListView.builder 索引和提供者 Package 从列表中删除项目 - Flutter - removing an item from a list using ListView.builder index and Provider Package 使用 Flutter 中的 package 与提供程序的连接 - Using connectivity package in Flutter with provider 如何在将显示为字符串并将其添加到文本元素以使用 Flutter 显示时从 List 中删除括号 [] - How to remove brackets [ ] from List when going to display as a String and add it to a text element for display using Flutter 使用 flutter package 在 Google 地图上添加和删除制造商 - Add and remove makers on Google maps using flutter maps flutter package 如何将项目添加到 Flutter 中的自定义列表? - How to add items to a custom list in Flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM