Migrating textFieldConfiguration and maxLengthEnforced to new version of flutter - flutter

My old code is
maxLengthEnforced: widget.textFieldConfiguration.maxLengthEnforced,
and it gives error
Error: No named parameter with the name 'maxLengthEnforced'.
Some guids say I shoud replace maxLengthEnforced by maxLengthEnforcement.
But the new code:
maxLengthEnforcement: widget.textFieldConfiguration.maxLengthEnforcement,
gives new error:
The getter 'maxLengthEnforcement' isn't defined for the class 'TextFieldConfiguration<dynamic>'.
What should I do?

As per documentation,
Added a maxLengthEnforcement parameter using the new enum type
MaxLengthEnforcement, as a replacement for the now-deprecated boolean
maxLengthEnforced parameter
Now when it was a boolean, true/false was sufficient in your code, but now it is an enum so when you replaced maxLengthEnforced with maxLengthEnforcement, you also need to make changes in textFieldConfiguration to adjust to this change. Simple replacement will not work, hence the error.

Related

Why getting The method 'contains' isn't defined for the type 'String'. flutter

I am using contains method for a string in dart but getting an error like this.
can anyone tell me what is wrong with using .contains for a string in dart
This is my code
String isPdf = "https://www.africau.edu/images/default/sample.pdf";
isPdf.contains("pdf");
Error occurs
The method 'contains' isn't defined for the type 'String'.
Try correcting the name to the name of an existing method, or defining a method named 'contains'.
In vscode quickfix tells me that
change the 'contains' method to 'oscontains'.

The method 'compareTo' can't be unconditionally invoked because the receiver can be 'null'

How to fix this? Error Message: The method 'compareTo' can't be unconditionally invoked because the receiver can be 'null'.
Try making the call conditional (using '?.') or adding a null check to the target ('!').
Second Error: The argument type 'Object?' can't be assigned to the parameter type 'Object'.
How to fixed it?
It looks like you lack basic understanding about null safety features.
String? basically means that it's a String that can be null. This can be done with any type. A question mark ? at the end of the type name makes it nullable, and if it isn't there it can't be null.
The language is designed in such a way that you can't assign any objects that are nullable to variables that aren't or use methods of objects that might be null.
There are several ways to handle this.
1) don't make them nullable in the first place.
In your example, whatever the object is, it must have some field declared as String? name. Just remove the ? if possible. There's a good chance it will make other errors pop up, but it should be solvable, at least in the case where you don't allow it to be null. If for whatever reason you need it to be nullable this is not a solution.
2) tell the compiler that you are sure it's not null in this situation
This is done by adding ! at the end of the variable. In your example that would be:
b.name!.compareTo(a.name!)
This is probably the easiest solution but the program will throw errors at runtime if they do happen to be null
3) provide a fallback value using ??
?? basically is an operator that returns the left side if it's not null and otherwise the right side. You can use this to your advantage to provide fallback values. In your example you could do:
(b.name ?? '').compareTo(a.name ?? '')
This way it takes empty string in case the name is missing. This would be a safer option compared to option 2.
4) conditional calling using ?.
Maybe not applicable in your situation but good to know anyway. Let's say some class A has the method getName and you have an object a of type A? and this code:
String? b = a.getName();
this is not allowed because a can be null. But you can write this:
String? b = a?.getName();
This basically mean that it executes getName() and assigns it to b when a is not null or just assigns null otherwise, which is possible because b is allowed to be null here. Therefore, this is NOT possible
String b = a?.getName();
Now b is defined as not being nullable and since the assignment possibly can provide null you are not allowed to.

Cannot convert type value of type Observable<Store> to expected argument type Observable<Store?>

I'm getting an error which says the following:
Cannot convert value of type 'Observable<Store>' to expected argument type 'Observable<Store?>
So the method I'm calling StoreMock(state: stateSubject, store: storeSubject) seems to expect an optional for the observable storeSubject. But I'm not sure why it should matter if I pass in a non-optional. Shouldn't it be fine as it will always be there.
Any ideas?
You have to wrap the object into an Optional:
StoreMock(state: stateSubject, store: storeSubject.map(Optional.some))
Just one call will fix this.

The name 'string' isn't a type and can't be used in an 'is' expression

During unit testing of a function returning different types of objects, I need to check if the type of returned object is the same as expected. Therefore, I need to pass multiple classes inside a variable. Then I need to use this variable with the is operator to check types.
final string = String;
assert('foo' is string);
But I am getting
error: The name 'string' isn't a type and can't be used in an 'is' expression.
I read somewhere that a library called Dart:mirrors can solve this problem but I haven't seen an actual example.
In unit testing, you know the expected answer. There shouldn't be a need to make your types variables.
Instead, just assert with the strong typed
assert('foo' is String);
I found the answer. The trick to create an instance of the type that I want to assert, then use runtimeType property.
If a class is called User from a.dart and another one is also called User from b.dart, runtimeType won't be the same
final string = 'anything'.runtimeType;
assert('foo'.runtimeType is string);

Bug: invalid underlying type name

If you change the 'Underlying type name' property of an enumeration in something that does not exist (typo). The application throws an 'CF0230: Invalid enumeration type 'System.Int32System.Int16'.' Error. Which is fine.
However, there is no way to change the type afterwards. I had to edit the CFP manually to correct the error.