Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not write to a list that contains map

Tags:

flutter

I want to mix a list that contains maps like this:

initTask() async {

   List tasks = await ourDb.rawQuery("SELECT * FROM level1");
   Random random = Random();
   tasks.shuffle(random);

}

and then I got this error:

E/flutter (15953): [ERROR:flutter/shell/common/shell.cc(184)] Dart Error: Unhandled exception:
E/flutter (15953): Unsupported operation: read-only

can anyone help?

like image 464
E. Spiegel Avatar asked Feb 06 '19 08:02

E. Spiegel


People also ask

What is the difference between a map and a list?

A Map is an object that maps keys to values or is a collection of attribute-value pairs. The list is an ordered collection of objects and the List can contain duplicate values. The Map has two values (a key and value), while a List only has one value (an element). So we can generate two lists as listed: List of keys from a Map.

How do you map a list in Python?

The map () function (which is a built-in function in Python) is used to apply a function to each item in an iterable (like a Python list or dictionary). It returns a new iterable (a map object) that you can use in other parts of your code.

How to map a list of values to a list?

In React, we can use the map () function to map a list of values to a list of components. Let’s see how we can create a list in react using a simple project.

How to map a list of objects to a list in react?

A map () is one such function that is used to create a list of objects by calling a function on each element of the array. In React, we can use the map () function to map a list of values to a list of components.


1 Answers

Even if you've solved your issue there is no accepted answers so I'm gonna try explaining the solution that was provided in the comment by pskink.

Usually data returned by a DB are immutable so by doing List tasks = await ourDb.rawQuery("SELECT * FROM level1"); you are getting an unmodifiable list reference. To be able to modify it you need to create a new list object from your original list.

You can either use List.of or List.from to generate a new modifiable list object containing all the items from your query.

List tasks = List.from(await ourDb.rawQuery("SELECT * FROM level1"));
List tasks = List.of(await ourDb.rawQuery("SELECT * FROM level1"));
like image 74
Guillaume Roux Avatar answered Oct 14 '22 16:10

Guillaume Roux