1

i want to create 4 new lists from a single list. i created this, in this if i do some changes in a single list it changed to all of them. i want to manipulate each list individually.

  List<DietModel> foodList = [];
  List<DietModel> breakFastList = [];
  List<DietModel> lunchList = [];
  List<DietModel> snacksList = [];
  List<DietModel> dinnerList = [];

 void getFoodList() async {
    foodList = await _dietPageRepositoryImpl.getAll();
    breakFastList =  foodList;
    lunchList = foodList;
    snacksList = foodList;
    dinnerList = foodList;
  }
Lalit Rawat
  • 1,022
  • 2
  • 17
  • 39
  • no its not working – Lalit Rawat Oct 09 '20 at 07:21
  • 1
    `void getFoodList() async` this declaration is never a good idea. Whatever your problem with lists is, this will sooner or later be a problem, too. `aysnc` methods return a `Future<>` even if it's just a `Future`. – nvoigt Oct 09 '20 at 07:31

3 Answers3

1

Use List.from(mynewlist) instead of mylist = mynewlist

If anyone knows a workaround for classes - that would be interesting!

w461
  • 2,168
  • 4
  • 14
  • 40
  • breakFastList = List.from(foodList); i tried this but not working – Lalit Rawat Oct 09 '20 at 07:22
  • @LalitRawat Repeatedly saying that it's "not working" is not helpful if you describe how it isn't working. What happened when you tried it? – jamesdlin Oct 09 '20 at 08:10
  • 1
    @w461 If the input and output `List`s are of the same type, [always prefer `List.of` instead of `List.from`](https://stackoverflow.com/a/50320697/). (Or [use `originalList.toList()` or `[...originalList]` as other ways to make copies](https://stackoverflow.com/a/61541310/).) – jamesdlin Oct 09 '20 at 08:12
  • @jamesdlin what is the reason for this? And since you seem more experienced, is there a way to create a cloned instance of a data class object? – w461 Oct 09 '20 at 09:45
  • 1
    The answers I linked to in my comment explain why you shouldn't use `List.from` for most situations. And no, there is no general way to clone objects. – jamesdlin Oct 09 '20 at 12:12
  • List.from(mynewlist) created me new 4 lists but the elements of the lists remain same thats the problem here. for now i fetched the data 4 times for each list and it is working for me – Lalit Rawat Oct 09 '20 at 13:05
  • I guess, I do not really understand your comment, but if you assign the same foodList to all 4 of them then you all will be the same – w461 Oct 09 '20 at 13:16
1

First you have to initialize your list like you did

List<DietModel> foodList;
List<DietModel> breakFastList;
List<DietModel> lunchList;
List<DietModel> snacksList ;
List<DietModel> dinnerList;

and then on the flutter initState you have to tell that they are a type of list.

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

    foodList = List();
    breakFastList = List();
    lunchList = List();
    snacksList = List();
    dinnerList = List();
  }

and in the end when you fetch your data you can copy your list data to other 4 lists with this.

void getFoodList() async {
    foodList = await _dietPageRepositoryImpl.getAll();

    breakFastList = List.from(foodList);
    lunchList = List.from(foodList);
    snacksList = List.from(foodList);
    dinnerList = List.from(foodList);
  }
Maz341
  • 2,232
  • 15
  • 20
1

By using breakFastList = foodList; you set breakFastList to be foodList, meaning that if foodList changes, breakFastList will also change.

By using breakFastList.addAll(foodList); you will add each item that exists in foodList to breakFastList individually. Meaning that if you delete an item from foodList, or add an item to foodList afterwards, breakFastList will remain the same.

kimeuvade
  • 221
  • 1
  • 8