How to create modifiable dart array - dart

I want to create a buffer that can appended things.
For example
var allInput = new Uint8List(1);
allInput.add(list)
But it's informed me that this is can not be modify.

Per the API docs, Uint8List is a fixed list. You could use code such as:
var allInput = new Uint8List(1);
allInput[0] = 123;
If you want a growable list, you could do something like:
var allInput = new List();
allInput.addAll(list);
or
var allInput = new List<int>();
allInput.addAll(list);
Essentially, if you provide a size specifier when creating a list, that makes it fixed size. Otherwise it's extendable (ref)

What Chris writes in his answer is correct today, but starting soon, the generic List constructor will not be fixed length when a length argument is given. The list’s size will still be changeable afterwards. There will also be a couple of additional named constructors for List:
factory List.fixedLength(int length, {E fill: null})
factory List.filled(int length, E fill)
these will help with constructing fixed length lists and creating lists with pre-filled values.
For details on bleeding edge changes to List, see:
http://api.dartlang.org/docs/bleeding_edge/dart_core/List.html

Related

How to add same value to a list multiple times in one go in Dart/Flutter

Let's say I have an empty list.
If I want to add a certain number of letters "g" to this list. for example 30 40 etc. the number I send may change.
Is there a way to do this in one go without using a loop?
What is it, if any?
https://api.flutter.dev/flutter/dart-core/List/fillRange.html
I need a method like fillRange.
FillRange does not work on an empty list.
If the list is empty, don't bother using it. Just generate a new list with List.filled:
final list = List.filled(30, 'g');
EDIT: For completeness, here is how to use the above with an existing list:
final list = <String>[...];
// Mutate original list
list.addAll(List.filled(30, 'g'));
// Create new list with spread syntax
final newList = [
...list,
...List.filled(30, 'g'),
];

Dart: Sorting by length of a list in a list of lists

I want a function that takes a list of lists. It should sort this list of lists, regardless of its type, by the length of each list within it.
I thought I could achieve this by using the function below, but I am getting type errors. X is not a subtype of Y. From my understanding, using dynamic means it can take any type, so what am I doing wrong?
List<List<dynamic>> sortByLength(List<List<dynamic>> lss) {
final newLss = List.from(lss);
return newLss..sort((a, b) => a.length.compareTo(b.length));
}
Your problem is that you accidentally converted a List<List<dynamic>> to a List<dynamic> and then tried to return that as a List<List<dynamic>> without a cast.
final newLss = List.from(lss);
This makes two mistakes:
It uses List.from instead of List.of (or instead of lss.toList() or [...lss]).
It does not specify an explicit type for the List.
Combined, those mistakes give newLss a type of List<dynamic>. Attempting to return newLss then fails because converting List<dynamic> to List<T> requires using List.cast to change the element type.

What is the difference between "var" and "List" in Dart?

Is there a noticeable difference between the two?
var example = ["some","content",11,45,true];
List example = ["some","content",11,45,true];
With var example the type (static and runtime) for example will be inferred from the assigned value ["some","content",11,45,true] which will be List (or actually List<dynamic>)
With List example the type will not be inferred but the explicitely provided type List (or actually List<dynamic> if no generic type is provided) will be used instead.
For var example = ["some","content","11","45","true"]; the inferred type would be List<String>.
As far as I know and as simple as I can be;
List is a data type just like some other built-in types in Dart such as String, int and bool. When you initialize a variable using List, the assigned value must be of List type. i.e. You cannot do this
List example = "sometext";
because you're trying to assign a String value to a List variable or object here.
Whereas, var is a way to declare a variable without specifying its type. For var will accept all kind of data types.
Is there a noticeable difference between the two?
var example = ["some","content",11,45,true];
List example = ["some","content",11,45,true];
Both the methods of declaration have same effect unless you expect to assign a value to example with type(s) other than List during it's lifetime. i.e If you're looking to assign an int or double or string or whatever value to example in future use the first method else you can use any one of them.

Dart: how to apply function to each inner element of list of lists

Obviously playing with dart and still new to it and implementing tic tac toe. So I have a list of lists of winning positions and want to check if any of the lists is sublist of the inputed moves list. Sounds pretty easy right, so I apply for each element of inner list I wanna check my condition. What are the dart ways to achieve this. This isn't working because map expect function of type void and I return some calls to print for each element of every inner list obviously. Why it isn't showing me any mistake at all.
I tried with .forEach and .takeWhile examples but coudn't get it working.
List <List<int>> winMoves = new List.generate(8, (i) => new List(3));
winMoves[0]=[1,2,3];
winMoves[1]=[4,5,6];
winMoves[2]=[7,8,9];
winMoves[3]=[1,4,7];
winMoves[4]=[2,5,8];
winMoves[5]=[3,6,9];
winMoves[6]=[1,5,9];
winMoves[7]=[3,5,7];
winMoves.map((list) => (list.forEach((el) => print(el))));
import 'package:collection/collection.dart' show ListEquality;
...
const eq = const ListEquality<int>();
print(winMoves.indexWhere((list) => eq.equals(list, [1, 5, 9])));
Depending on what result you want you can also use where, firstWhere, any, ...
There are many ways to solve this problem, depending on what the exact desired result is.
You have a list of elements, and you want to check something about these elements (in this case the elements are lists too, but that's not important to begin with).
If you want to know whether any of the elements satisfies your check, you can use:
bool hasWin = winMoves.any((moves) => ...check...);
If you want to find an element which satisfies your check, you can use:
var win = winMoves.firstWhere((moves) => ...check..., orElse: null);
If you want to find all the winners:
var wins = winMoves.where((moves) => ...check...).toList(); // `where` is lazy.
How to write the check is a separate issue. In this case you seem to want, as Günther Zöchbauer has already written, something like
(moves) => moves.every(expectedMoves.contains)
Your example is not giving you any warnings or errors because it's correct, and it doesn't actually do anything. The map function is lazy, it creates an Iterable which won't do anything until you start iterating. If you call .toList() on it, it'll create a List<void>. Not very useful, but valid in Dart 2.
Also, your initial winMoves initialization is overly complex. You can just do:
List<List<int>> winMoves = []..length = 8; // for growable list, or
List<List<int>> winMoves = List(8); // for fixed-length list.
You actively fill the list with lists, then immediately overwrite those values with new lists in the following lines.

How can i add an entry to a specific index in a list?

I can call list.add() which adds at the end but there is no convenient way to add an entry to a specific index which at the same time grows the list.
Dart 2
var idx = 3;
list.insert(idx, 'foo');
Depends on whether you want to insert a single item or a bunch of items
https://api.dartlang.org/stable/2.1.0/dart-core/List/insert.html
https://api.dartlang.org/stable/2.1.0/dart-core/List/insertAll.html
All available methods https://api.dartlang.org/stable/2.1.0/dart-core/List-class.html#instance-methods
You can use insertRange, it will grow the list when adding new elements.
var list = ["1","3","4"];
list.insertRange(0, 1, "0");
list.insertRange(2, 1, "2");
list.forEach((e) => print(e));
You can try it out on the DartBoard here
You can use the insert() and removeAt() methods now.
Ok, it seams as if list.insertRange(index, range, [elem]) is what i am looking for.
you can use
void insert(int index, E element);
Inserts all objects of [iterable] at position [index] in this list.
This increases the length of the list by the length of [iterable] and
shifts all later objects towards the end of the list.
The list must be growable.
The [index] value must be non-negative and no greater than [length].

Resources