This question already has answers here:
The default 'List' constructor isn't available when null safety is enabled. Try using a list literal, 'List.filled' or 'List.generate'
(4 answers)
Closed 1 year ago.
After upgrading to the latest version of flutter, I get a deprecation warning for all my Lists.
List<MyClass> _files = List<MyClass>();
=>'List' is deprecated and shouldn't be used.
Unfortunately, it does not give a hint of what to replace it with.
So what are we supposed to use instead now?
Dart SDK version: 2.12.0-141.0.dev
Flutter: Channel master, 1.25.0-9.0.pre.42
Ok, found it, it's just how to instantiate it:
List<MyClass> _files = [];
Edit: maybe the most common ones, a bit more detailed according to the docs:
Fixed-length list of size 0:
List<MyClass> _list = List<MyClass>.empty();
Growable list:
List<MyClass> _list = [];
//or
List<MyClass> _list = List<MyClass>.empty(growable: true);
Fixed length with predefined fill:
int length = 3;
String fill = "test";
List<String> _list = List<String>.filled(length, fill, growable: true);
// => ["test", "test", "test"]
List with generate function:
int length = 3;
MyClass myFun(int idx) => MyClass(id: idx);
List<MyClass> _list = List.generate(length, myFun, growable: true);
// => [Instance of 'MyClass', Instance of 'MyClass', Instance of 'MyClass']
List<MyClass> myList = <MyClass>[];
From:
_todoList = new List();
Change to:
_todoList = [];
old version
List<Widget> widgetList = new List<Widget>();
new version
List<Widget> widgetList = [];
Related
I have a function that returns List But in my case I want to read and display float values. However, this function is a system function I can't update it.
My question is how to convert List to List?
This is the code:
characteristic.value.listen((event) async {
var bleData = SetupModeResponse(data: event);
});
Event is by default a List. When I try to declare data as List; I got List cannot assigned to List.
I would be very thankful if you can help me.
you can use the map method on list
like that:
List<int> intList = [1, 2, 3];
List<double> doubleList = intList.map((i) => i.toDouble()).toList();
You can learn more about dart list mapping here map method
This should also work:
List<int> ints = [1,2,3];
List<double> doubles = List.from(ints);
Yo can try this method and see if it works
List<int> num = [1,2,3];
List<double> doubles = List.from(num);
Try the following code:
List<double> doubleList = event.map((i) => i.toDouble()).toList()
actually I want to convert a list of decimal to a list of hexadicimal. I tried .toRadixString(16) But I got : The method 'toRadixString' isn't defined for the type 'List'..
this is my code:
BehaviorSubject<List<int>> _value;
Stream<List<int>> get value => Rx.merge([
_value.stream,
_onValueChangedStream,
]);
List<int> get lastValue => _value.value ?? [];
Future<Null> write(List<int> value, {bool withoutResponse = false}) async {
final type = withoutResponse
? CharacteristicWriteType.withoutResponse
: CharacteristicWriteType.withResponse;
var request = protos.WriteCharacteristicRequest.create()
..remoteId = deviceId.toString()
..characteristicUuid = uuid.toString()
..serviceUuid = serviceUuid.toString()
..writeType =
protos.WriteCharacteristicRequest_WriteType.valueOf(type.index)!
..value = value.map((e) => e.toRadixString(16)).toList();
// Uint8List(4)..buffer.asInt32List()[0]=value;
//..value = value.toRadixString(16);
I would be very thankful if you can give me a solution for converting this list from decimal or int to hexadicimal.
[1]: https://i.stack.imgur.com/MVOkQ.png
You are trying to use toRadixString on list.
as on https://api.flutter.dev/flutter/dart-core/int/toRadixString.html:
Converts this to a string representation in the given radix.
as in documentation you should use toRadixString on int.
in your case you can try this:
List get hexLastValue => _value.value.map((e) => e.toRadixString(16)).toList();
This question already has answers here:
The default 'List' constructor isn't available when null safety is enabled. Try using a list literal, 'List.filled' or 'List.generate'
(4 answers)
Flutter: List is deprecated? [duplicate]
(4 answers)
Closed 1 year ago.
List has been deprecated. How do I re-write the following code?
RosterToView.fromJson(Map<String, dynamic> json) {
if (json['value'] != null) {
rvRows = new List<RVRows>();
json['value'].forEach((v) {
rvRows.add(new RVRows.fromJson(v));
});
}
}
According to the official documentation:
#Deprecated("Use a list literal, [], or the List.filled constructor instead")
NOTICE: This constructor cannot be used in null-safe code. Use List.filled to create a non-empty list. This requires a fill value to initialize the list elements with. To create an empty list, use [] for a growable list or List.empty for a fixed length list (or where growability is determined at run-time).
You can do this instead:
RosterToView.fromJson(Map<String, dynamic> json) {
if (json['value'] != null) {
rvRows = <RVRows>[];
json['value'].forEach((v) {
rvRows.add(new RVRows.fromJson(v));
});
}
}
Another option is:
List<RVRows> rvRows = [];
Instead of:
rvRows = new List();
Write:
rvRows = [];
The error message tells you what to do. When I run dart analyze, I get:
info • 'List' is deprecated and shouldn't be used. Use a list literal, [],
or the List.filled constructor instead at ... • (deprecated_member_use)
Try replacing the use of the deprecated member with the replacement.
error • The default 'List' constructor isn't available when null safety is
enabled at ... • (default_list_constructor)
Try using a list literal, 'List.filled' or 'List.generate'.
The documentation for the zero-argument List constructor also states:
This constructor cannot be used in null-safe code. Use List.filled to create a non-empty list. This requires a fill value to initialize the list elements with. To create an empty list, use [] for a growable list or List.empty for a fixed length list (or where growability is determined at run-time).
Examples:
var emptyList = [];
var filledList = List<int>.filled(3, 0); // 3 elements all initialized to 0.
filledList[0] = 0;
filledList[1] = 1;
filledList[2] = 2;
var filledListWithNulls = List<int?>.filled(3, null);
var generatedList = List<int>.generate(3, (index) => index);
You also could use collection-for for both cases:
var filledList = [for (var i = 0; i < 3; i += 1) 0];
var filledListWithNulls = <int?>[for (var i = 0; i < 3; i += 1) null];
var generatedList = [for (var i = 0; i < 3; i += 1) i];
I feel like I missed something, as this question seems so easy to answer. However, I haven't found anything on stack overflow or in the dart docs.
What I've tried:
var list = Map<int, List<int>>();
list[0] = [];
or
var list = Map<int, List<int>>();
list[0] = List<int> listOne;
The docs didn't help much:
https://dart.dev/guides/language/language-tour#maps
And a similar question doesn't seem to exist.
How can I solve this problem. Do I need to use a workaround?
Greetings.
Map of int and list
var listMapping = Map<int, List<int>>();
listMapping[0] = [];
listMapping[33] = [12, 0, 345, -23, 999999];
listMapping[45] = List<int>();
listMapping[45].add(101);
listMapping[45].add(109);
print(listMapping[33]);
// A map doesn't store things in a asequencial data structure e.g. List or array
// Hence it doesn't implement Iterable and hence it can not be traversed like below
/*for(List<int> list in listMapping){
print(list);
}*/
List of List
If the integer key in your case is just to store the index and will always be sequencial e.g. 0,1,2,3... then a better solution would be a List of a Lists
var myLists = List<List<int>>()
myLists.add([1,2,45,56,78]);
var aNewList = [22,33,44,55];
myLists.add(aNewList);
The first one looked fine
Im not sure if I am understanding you correctly, but your first attempt worked for me in the DartPad. I only renamed it to map, as it is a map which contains lists.
var map = Map<int, List<int>>();
map[0] = [];
You could also declare it as a final, as the variable itself doesn't get reassigned
final map = Map<int, List<int>>();
map[0] = [];
Give it a type annotation, if it helps you
final Map<int,List<int>> map = Map<int, List<int>>();
map[0] = [];
The second code snippet does not work
var list = Map<int, List<int>>();
//you cannot name a value you want to assign
list[0] = List<int> listOne;
Maybe this is what you were going for:
var list = Map<int, List<int>>();
final listOne = <int>[];
list[0] = listOne
Map literals
This is the way I would do it
final map = {0: []};
//or for added clarity
final Map<int,List<int>> map = <int,List<int>>{0: []};
Simple operations with the map and its keys (int) and values (List)
//assign new array to different keys
map[0] = [0,1,2,3,4];
map[2] = <int>[];
map[4] = <int>[0,3];
//remove all values with an array length smaller than two
map.removeWhere((key,value)=>value.length<2);
//remove value at key 0
map.remove(0);
//clear map
map.clear();
[NOTE] If you want to reassign a completely new map to map, you would have to declare it non final
Map<int, List<int>> map = Map<int, List<int>> ();
map[0] = [10,0];
map[1] = [];
Learn more about dart maps
Is there any way to find unique values between two lists without using a loop?
List<String> first = ['A','B','C','D'];
List<String> second = ['B','D'];
I need the result to be like this:
result = ['A','C'];
You can use where() with contains() methods from List:
void main() {
List<String> first = ['A','B','C','D'];
List<String> second = ['B','D'];
List<String> result = first.where((item) => !second.contains(item)).toList();
print(result); // [A, C]
}
Edit in DartPad.