Dart Default Nullable Parameters - flutter

I've recently been migrating my Flutter/Dart apps to be null safety compatible/sound. To this end I came across a situation I can't really figure out a 'best practice' for.
I have a few functions similar to the following:
String defaultErrorMessage({String? message = 'Please try again later.'}) {
return "Error. $message";
}
Fairly straightforward - just meant to standardize error messages and provide a default one for situations where there may not be an error message.
The question is this:
The parameter 'message' is not required and has a default value. If I pass it a null string it will simply print "Error. null" - this is shown in this dartpad
Is there a simple way or special syntax I should be using for default parameters? I recognize there are simple ways to rewrite this I feel like I shouldn't have to - isn't the point of all these null features/checks to automatically do things like this? Specifically I could do the following:
String defaultErrorMessage({String? message}) {
message ??= 'Please try again later.';
return "Error. $message";
}
But I'd much prefer to handle that in the function definition itself.
Thank you in advance for the help!

So basically, you want to make it so that if you pass a null value, it uses the default message you have set? I am sorry to say that the code you already posted is probably the best to do that.
But if you want null values to be the same as passing no value, why even make the variable nullable at all? Would it not make more sense to do something like this?
String defaultErrorMessage({String message = 'Please try again later'}) {
return "Error. $message";
}
Which will of course give you some compilation errors if someone did something like this:
defaultErrorMessage(message: null);
so you will have to go to every time this happened and change it to
defaultErrorMessage();
But well, that's kinda how null safety migration has always gone for me

Related

FLUTTER - Getting this null check error, despite having null safety through out the code

Getting this null check error, despite having null safety through out the code.
I'm attaching a git repo
https://github.com/HidayatBukhari01/Notes-App.git
can someone please check this code and explain why am i having this errorenter image description here
On clicking + icon data should have been inserted into notes table, instead it's throwing null safety error!.
You need to check, if the value becomes null, if it does, the above error is evident. Else, you need to put the "?" operator, before the variable or function where the above error occurs, as -
type ?<varname> = <Text here>.
Even I am new to Flutter. if you get this error resolved, great, else check out on other's answers
I went through your code and here is the mistake you done
dbHelper!
Here you said that said that dbHelper is not null but the value its getting is null and before calling this check its value in console first,
and use it like this only
dbHelper

What is the correct way to express "select all when nothing is specified in parameter"?

Let's say we have an HTTP endpoint to get all elements by name?
GET /elements?name={name}
{name} can have a value of CSV or be absent
valid:
GET /elements?name=Bill,Mary,Ann
GET /elements?name=Mike
GET /elements
invalid:
GET /elements?name=
Somehow we find out in controller that name is not passed. We know that the contract implies to return all values for elements. Possible decisions on further actions (I've seen in different projects) are:
using a NULL or a "dummy" substitution like a secret char sequence "!#$#%#$" and juggling them in database while building a query
using if (present) { executeQueryA } else { executeQueryB } logic
I am not sure I like either of these approaches, because when there is more than one optional filter these designs become unmaintainable. Something makes me believe that there is a better way to handle the situation.
What would be a proper design on back-end and in database query to handle the case "select all" when nothing is given? Just a general idea and some pseudo-code will be much appreciated.

Is there a way to change what the .operator "string" will be conditionally

This is going to sound very weird but it will make the code for this app very compact.
Is there a way to call a .operator conditionally. Here is an example
say I have a class with three values that I can do a . on.
class A {
int intOne;
int intTwo;
int intThree;
}
to get the intOne in class A you can do A.intOne right....
but what if you you wanted the string at the right of the . to conditionally be there.
A."conditionalvariable"
so if a user clicks a button say button A then the conditional variable will be A.intOne.
and if the user clicks button b then the value of "conditionalvariable" will be intTwo and therefore you will be getting the data.
please assume that I am not stupid, and I am needing this exact thing for a specific use case. If you post an answer that is not this exact solution it will not be accepted.
example:
var a;
switch (name) {
case "intOne":
a = A.intOne;
break;
...
}
//this is not an acceptable answer
I realize that this would be an "answer" to this question, but it actually isn't I need the exact thing stated because I am using streams.
here is that use case I was talking about. I could explain away all day as to why I need it this way, but either way this either does exist or doesn't so a simple no this doesn't exist is an acceptable answer.
return StreamProvider.value(
value: classDataNotif.homework,
)
based on what is clicked before this widget the "homework" will need to change to "notes", or "tests".
You cannot do a switch above this because it will be calling the stream to early and cause the widget to crash. doing a switch inside the widget and copying code over would defeat the purpose. in order to make the code as compact and as easy to write as possible i need the string at the right of the . operator to change.
thanks in advance :)

Possible to isElementPresent(:id, "id") in watir webdriver

Using Watir Webdriver, I wanted to have a helper that would check for any element with given id. I may not know what type it is ( button or link or text). Can I just do
browser.Element(:id, id).exists
All of the examples i've found on google check against a specific element type, as in
browser.button(:id," ").exits
If there is a way, please share the syntax.
In Watir-Webdriver, I would use something like this:
browser.element(class: 'post-tag').exists?
which would find the watir-webdriver tag on this page and report that it exists. Note that I used the 1.9 syntax instead of the alternative syntaxes of:
browser.element(:class => 'post-tag').exists?
or
browser.element(:class, 'post-tag').exists?
As Dave points out, there is #element method. (You were wrong just in capitalization, it is not #Element.)
Since you are asking about accessing it using id attribute, try this:
browser.element(:id => id)
I've never gotten .exists? to work right on it's own.
What I've had to use in these cases has been to explicitly validate the "exist?"... like:
cf_checbox = #browser.text_field(:id=>'continue-ring', :value=>true).exists?
assert( cf_description == true)
without that explicit assertion, I would always get a "true" even when the value didn't exist.

Get statuscode text in C#

I'm using a plugin and want to perform an action based on the records statuscode value. I've seen online that you can use entity.FormattedValues["statuscode"] to get values from option sets but when try it I get an error saying "The given key was not present in the dictionary".
I know this can happen when the plugin cant find the change for the field you're looking for, but i've already checked that this does exist using entity.Contains("statuscode") and it passes by that fine but still hits this error.
Can anyone help me figure out why its failing?
Thanks
I've not seen the entity.FormattedValues before.
I usually use the entity.Attributes, e.g. entity.Attributes["statuscode"].
MSDN
Edit
Crm wraps many of the values in objects which hold additional information, in this case statuscode uses the OptionSetValue, so to get the value you need to:
((OptionSetValue)entity.Attributes["statuscode"]).Value
This will return a number, as this is the underlying value in Crm.
If you open up the customisation options in Crm, you will usually (some system fields are locked down) be able to see the label and value for each option.
If you need the label, you could either do some hardcoding based on the information in Crm.
Or you could retrieve it from the metadata services as described here.
To avoid your error, you need to check the collection you wish to use (rather than the Attributes collection):
if (entity.FormattedValues.Contains("statuscode")){
var myStatusCode = entity.FormattedValues["statuscode"];
}
However although the SDK fails to confirm this, I suspect that FormattedValues are only ever present for numeric or currency attributes. (Part-speculation on my part though).
entity.FormattedValues work only for string display value.
For example you have an optionset with display names as 1, 2, 3,
The above statement do not recognize these values because those are integers. If You have seen the exact defintion of formatted values in the below link
http://msdn.microsoft.com/en-in/library/microsoft.xrm.sdk.formattedvaluecollection.aspx
you will find this statement is valid for only string display values. If you try to use this statement with Integer values it will throw key not found in dictionary exception.
So try to avoid this statement for retrieving integer display name optionset in your code.
Try this
string Title = (bool)entity.Attributes.Contains("title") ? entity.FormattedValues["title"].ToString() : "";
When you are talking about Option set, you have value and label. What this will give you is the label. '?' will make sure that the null value is never passed.