Avoid "value can be null" warning for extension method - flutter

I am using an extension on Iterable that adds the following two methods
extension MyIterableExt<T> on Iterable<T> {
bool get isEmptyOrNull => this == null || this.isEmpty;
bool get isNotEmptyOrNull => this != null && this.isNotEmpty;
}
This works as I expected, but when I try to use it in statements where the receiver can be null, I get a warning in IntelliJ. For example, the following code works and prints true:
List<String> x;
print('${x?.reversed.isEmptyOrNull}');
Is there any way I can make the Dart Analyzer understand that the extension checks if this == null and thus not show the warning?
Please note that I do not want to have to add suppression directives to every file or line where I use this extension!
I know I can add // ignore_for_file: can_be_null_after_null_aware or // ignore: can_be_null_after_null_aware to make the warning go away, but what I'm looking for is a way to make the Dart Analyzer understand that the warning for this particular extension method is not needed.
This is in a non null-safe project, if that matters.

you can make Iterable Nullable
extension MyIterableExt<T> on Iterable<T>? {
bool get isEmptyOrNull => this == null || this!.isEmpty;
bool get isNotEmptyOrNull => this != null && this!.isNotEmpty;
}

Make extension base on Iterable? is the way, if you have to find something to disable the warning but not want to change the code then im sorry cause i never do that before.
void main() {
Iterable? a = [];
Iterable b = ['1', '2', '3'];
print('a.isEmptyOrNull: ${a.isEmptyOrNull}');
print('b.isEmptyOrNull: ${b.isEmptyOrNull}');
print('a.isNotEmptyOrNull: ${a.isNotEmptyOrNull}');
print('b.isNotEmptyOrNull: ${b.isNotEmptyOrNull}');
}
extension on Iterable? {
bool get isEmptyOrNull => this == null || this!.isEmpty;
bool get isNotEmptyOrNull => this != null && this!.isNotEmpty;
}
result
a.isEmptyOrNull: true
b.isEmptyOrNull: false
a.isNotEmptyOrNull: false
b.isNotEmptyOrNull: true

Based on the Flutter docs, you should be able to add a comment at the top of your code that will essentially disable null safety checks:
// #dart=2.9
import 'src/my_app.dart';
main() {
//...
}
You should be able to put that comment at the top of any specific file you want this behavior. It doesn't have to be in your "main" file.

Related

flutter null safety from prefs.setStringList -> error : the argument list<String?> can't be assigned to the parameter type List <String>

Hello I try to make null safety migration, but have a probleme with
prefs.setStringList("save_list_autre2",save_list.toList());
the error is : the argument list<String?> can't be assigned to the parameter type List
RegExp regExp = new RegExp(r'(...)');
var save_list= regExp.allMatches(decrypted).map((z) => z.group(0));
if(regExp.allMatches(decrypted)==null ){
[...]
} else {
prefs.setStringList("save_list_autre2",save_list.toList());
}
I add .toString() from save_list= regExp.allMatches(decrypted).map((z) => z.group(0).toString());
the error is removed but I don't know if it's the good solution
When you call z.group(0) the group method could return null if there was no group with the given index (0 in this case). so dart thinks you might have some null values on your list of strings.
The reality is that you shouldn't get any null values because you are passing a 0. So to tell dart that the method isn't returning null, use the ! operator:
regExp.allMatches(decrypted)
.map((z) => z.group(0)!)
If, by some chance, you happened to have any null values, this code will not work (as I said, I believe you won't but what do I know? I've never used regexes with dart)
If that's the case, you first need to filter the null values and then use the !:
regExp.allMatches(decrypted)
.map((z) => z.group(0))
.where((v) => v != null) // filter out null values
.map((v) => v!); // let dart know you don't have any null values.
Hopefully that's enough to fix the problem

empty form text filed

I'm using a FormTextField in a Flutter app
To update a certain column value, the user types in the FormTextField, otherwise leaves the field empty.
I tried this code, but it was adding a null value to the column, deleting the existing value. I'm not happy with this behavior.
String _getProd5Name() {
if ((_prod5Controller.text).isNotEmpty == true) {
_prod5String = _prod5Controller.text;
}
return _prod5String;
}
Is there a way to do it?
I found similar questions, but they are relevant to other languages and their solutions don't solve my case.
String _getProd5Name() {
// Actually you don't have to make it private
// since this is a local variable inside a function
String _prod5String = variableContainingInitialValue;
if (_prod5Controller.text.isNotEmpty) {
_prod5String = _prod5Controller.text;
}
return _prod5String;
}
Here is my advice, since I love wrapping everything on 1 line. You can change the "" part with your result expectation. It's the same with your logic but it's shorter and instead of returning null I make it returning the empty string "". And also (_prod5Controller.text).isNotEmpty == true you can just shorten it to (_prod5Controller.text).isNotEmpty because .isNotEmpty always returning boolean true/false and if-else consuming boolean
String _getProd5Name() {
return ((_prod5Controller.text).isNotEmpty) ? _prod5String = _prod5Controller.text : "";
}

I have a query that returns '[ ]'. I want it to be shown on a widget as not having any value, how do I do this?

I have a query in my flutter app that should return empty if 'archived' 'isNull' is set to true. I accomplished that, but the issue I'm having now is the fact that the query returns empty (i.e [ ]). And on the widget, I want this to be shown as not having any value but instead, an empty pair of brackets '()' keep showing up.
I have tried checking for when query data equals null, something like this:
if (queryResult.data == null){
// The idea is for it not to just return '()' as the value
value = null;
}
else{
value = queryResult.data;
}
And also:
if (queryResult.data == []){
value = null;
}
else{
value = queryResult.data;
}
How do I go about this? Thanks in advance.
If "data" is a list why not try data.isEmpty() ? And also if you only need to avoid the brackets (dirty fix alert ⚠️) whatever you did it's returning a string value '()' then you check that
if(yourResults == '()' )value == null;
And please add more details for a better answer .

Xamarin.UITest-How to verify element is enabled or disabled

I am new to xamarin uitest, can any one please help me out how to verify the element is enabled or disabled with an example.
Thanks in advance.
Assuming that you are using Xamarin's Repl then you can use the following
app.Query(c => c.Id("ElementID")).FirstOrDefault().Enabled
Repl will then return whether the element's enabled property is either true or false
From this, then you can then assign that line to a variable and assert against it
var elementEnabled = app.Query(c => c.Id("ElementID")).FirstOrDefault().Enabled;
assert.AreEqual(true, elementEnabled);
I didn't really understand your question, but if what you're asking for is how to check if a toggle button is enabled, then you can go that way :
internal void SetToggle(string id, bool setToggled)
{
if (setToggled != IsToggled(id))
app.Tap(e => e.Property("id").Like(id));
}
internal bool IsToggle(string id)
{
return app.Query(e => e.Property("id").Like(id).Invoke("isChecked").Value<bool>())[0];
}

TYPO3 CMS 7.6 LLL translation is not working for default

am working on a field which should have a default based on LLL translations.
'default' => 'LLL:EXT:myext/Resources/Private/Language/Backend.xlf:field.myfield.default',
I Would expect the value defined in the Translation, but it contains the string ''LLL:EXT:myext/Resources/Private/Language/Backend.xlf:field.myfield.default'. How can I use the LLL Translation to define the Default values in TCA?
cu n00n
That is only possible by a custom FormProvider as the one of the core does not support this yet. Checkout the code
// Special handling for eval null
if (!empty($fieldConfig['config']['eval']) && GeneralUtility::inList($fieldConfig['config']['eval'], 'null')) {
if (// Field exists and is set to NULL
array_key_exists($fieldName, $databaseRow)
// Default NULL is set, and this is a new record!
|| (array_key_exists('default', $fieldConfig['config']) && $fieldConfig['config']['default'] === null)
) {
$newRow[$fieldName] = null;
} else {
$newRow[$fieldName] = (string)$fieldConfig['config']['default'];
}
} else {
// Fun part: This forces empty string for any field even if no default is set. Unsure if that is a good idea.
$newRow[$fieldName] = (string)$fieldConfig['config']['default'];
}
You can open an issue at https://forge.typo3.org/projects/typo3cms-core/issues.
In the meantime, I did a small extension which makes it possible to use the desired feature. This can be found here: https://github.com/georgringer/defaultlll.