Null Safety equivalent of "List<List<T>>()"? [duplicate] - dart

This question already has answers here:
How to replace deprecated List [duplicate]
(3 answers)
Closed 1 year ago.
My old working code:
List<List<T>> pages = List<List<T>>();
Now doesn't work with null safety:
The default 'List' constructor isn't available when null safety is enabled.
Try using a list literal, 'List.filled' or 'List.generate'.dartdefault_list_constructor
'List' is deprecated and shouldn't be used. Use a list literal, [], or the List.filled constructor instead.

// #dart=2.12
void doSomething<T>() {
List<List<T>> pages = [];
print(pages.runtimeType.toString());
}
void main() {
doSomething<String>();
}
Result
JSArray<List<String>>

Related

Dart Operators : The name 'b' isn't a type and can't be used in an 'is' expression [duplicate]

This question already has answers here:
What is the difference between 'is' and '==' in Dart?
(2 answers)
Closed 11 months ago.
I'm new to dart and reading about dart operators. In the book fluter in Action by Eric Windmill (Chapter 2, 2.24, page 34) the auther says:
is and is! verify that two objects are of the same type. They are equivalent to == and !=.
Trying to implement this as shown in the code below
void main() {
int a = 7;
int b = 2;
bool z = a == b; // It works when I use the equals symbol
print('Result: $z');
}
But when I use the ```is`` keyword, I get an error
void main() {
int a = 7;
int b = 2;
bool z = a is b; // It doesn't work here
print('Result: $z');
}
Error
The name 'b' isn't a type and can't be used in an 'is' expression.
Try correcting the name to match an existing type.
Not sure what the context of that statement is but is and is! is not the same as == and != in the sense that they does the same. I guess what the want to explain, is that the opposite of is is is! like the opposite of == is !=.
== is used to check if two objects are equal (based on what this object at left side defines to be equal). So for int we return true when using == if both numbers have the same numeric value.
The is operator is used to test if a given object does implement a given interface. An example:
void main() {
Object myList = <int>[];
if (myList is List<int>) {
print('We have a list of numbers.');
}
}
The error you are getting are telling you that for using is you need to provide an actual type at the right side of the is and not an object instance.

How to make a subscope in Swift? [duplicate]

This question already has answers here:
How to create local scopes in Swift?
(5 answers)
Closed 2 years ago.
In C I can insert pairs of brackets to indicate a subscope.
int main() {
int x = 5;
{
int y = 6;
x += y; //Works
}
x = y+8; //Uh oh
}
In Swift, if I try something similar it will assume I'm trying to call the previous statement as a function, or errors for other reasons. How do I simply make a subscope in Swift, to deallocate variables earlier?
You can try
do {
// insert sub scope code
}

Swift: Determine if array contains array [duplicate]

This question already has answers here:
Why can't I check to see if my array of arrays contains a specific array?
(2 answers)
Closed 7 years ago.
Im trying to figure out how to check if array contains another array:
var grid: [[Int]]!
grid = []
grid.append([1,1])
if grid.contains([1,1]) {
}
but the line
if grid.contains([1,1]) {
throws the error:
Contextual type '#noescape ([Int]) throws -> Bool' cannot be used with
array literal
Swift arrays doesn't conform to Equatable by default. But you can still compare them in the predicate:
if (grid.contains { $0 == [1,1] } == true) {
}

Issue with calling function in swift [duplicate]

This question already has answers here:
Why does a function call require the parameter name in Swift?
(6 answers)
Closed 8 years ago.
I was following optional values tutorial, in which we have following method
func findIndexOfString (string : String, array : String[]) -> Int?{
for (index, value) in enumerate(array){
if(value == string){
return index
}
}
return nil
}
however if i call this method by
let indexFound = findIndexOfString("myString", neighbour) //neighbour is array of String
give error that "Missing argument label ''array" in call , means i have to call this by
let indexFound = findIndexOfString("myString", array:neighbour)
Is it made compulsory to mention argument label in call?
Yeah. It is compulsory for for class methods. You should use the parameter names except for the first parameter. There comes the difference between the class methods and functions, for functions you will not use(You cant unless function defines an external parameter name) the parameter names.

Error :- the cast to value type 'Int32' failed because the materialized value is null [duplicate]

This question already has answers here:
The cast to value type 'Int32' failed because the materialized value is null
(8 answers)
Closed 6 years ago.
I have the following query:
int? Giver = Convert.ToInt32(db.vwAssignments.Sum(a => a.Amount));
but if there is no records that matches the search criteria then the following error will be raised
The cast to value type 'Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
pls help
The basics of defensive programming:
first assign the result of your Sum() operation to a nullable variable
object resultOfSum = db.vwAssignments.Sum(a => a.Amount);
check for NULL! and only if it's not null, then convert it to an INT
if(resultOfSum != null)
{
int Giver = Convert.ToInt32(resultOfSum);
}
Also: if your Amount field is a Int to start with, most likely this will already give you NULL or a valid Int - I think that call to Convert.ToInt32() is not necessary:
int? Giver = db.vwAssignments.Sum(a => a.Amount);
if(Giver.HasValue)
{
......
}

Resources