'Flutter(Dart) cannot remove from unmodifiable list
I am using tinder swipe cards. It has the following property:
swipeCompleteCallback:
(CardSwipeOrientation orientation, int index) {
updateStack(index);
},
),
updateStack function:
class _TinderCardsState extends State<TinderCards>
with TickerProviderStateMixin {
List lstUsers = [];
int lstUsersLength = 0;
@override
void initState() {
super.initState();
setState(() {
lstUsers = users;
lstUsersLength = users.length;
});
}
void updateStack(int index) {
setState(() {
lstUsersLength = lstUsersLength - 1;
lstUsers.removeAt(index);
});
}
So basically just trying to remove the top card from the stack. I am getting the above error even when it is a stateful widget. Why am I getting this error?
The data (just a dart file with a list, no class or anything):
const List users = [
{
"id": "p1",
"name": "John Doe",
"age": "44",
"imageUrl":
"assets/images/anastasia-vityukova-unsplash-blackandwhiteimage.png",
"profession": "Financial Consultant"
},
....
]
Solution 1:[1]
Heres a simple solution
Since your list is immutable you must create a new list but don't make it an instance if the original just make a copy.
List<OnlineChannelUser> userList = List.from(response.data);
userList.removeWhere((item) => item.uid == currentUid);
print(userList); //new modified list
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Londokuhle |
