variable changing property is not working in dart programming - flutter

void main() {
var a,b;
print("Enter the value of a & b : ");
a=int.parse(stdin.readLineSync());
b=int.parse(stdin.readLineSync());`enter code here`
print("Addition: ${a+b}");
print("Subtraction: ${a-b}");
print("Multiplication: ${a*b}");
print("Division: ${a/b}");
print("Mod: ${a%b}");
}
above code I have using for dart. my problem is whenever I pretend to change the variable to a integer or double value not allowing to run:
Error: The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't b=int. parse(stdin. read Line Sync());
above kind of error throwing. please can anyone help me to understand. because maybe I have made a wrong input or missed something. thank you in advance.

It's a error related to the new Null Safety in Flutter, your variable is nullable because it has the "?" after "String". You just have to add a "!" after the variable that's throwing the error.

Related

What does the '!' at the end of my code do?

Text(locations[index].location!)
without the '!' I get the next error:
The argument type 'String?' can't be assigned to the parameter type 'String'
Thanks in advance!
locations[index].location can return null, using ! we are saying, the value of it won't be null. Text widget doesn't accept null value.
It is better to do a null check 1st on nullable data like
if (locations[index].location != null) Text(locations[index].location)
or you can provide default value
Text(locations[index].location ?? "got null")
You can also do
Text("${locations[index].location}");
The exclamation mark (!) in Dart tells the code that you are sure this variable can never be null. It is an unsafe way of dealing with variables that may be null.
In your case, the type of the variable is String?, which means that it may be null or a String.
To safely unwrap a nullable variable, it is advised to do the following:
if (locations[index].location != null) {
//Do your logic here
}
Null Safety Documentation
It means it is a nullable type, which means it can be initialized without any value.

Can't assign non-nullable type to a nullable one

error: The argument type 'Future<List<GalleryPictureInfo>>' can't be assigned to the parameter type 'Future<List<GalleryPictureInfo>>?'.
Is this Dart Analysis or me? The project still compiles.
Upd. Added code example
FutureBuilder<List<GalleryPictureInfo>>(
future: derpiService.getListOfImages(),
//other code
);
#override
Future<List<GalleryPictureInfo>> getListOfImages(arguments) async {
List<GalleryPictureInfo> listOfImages = [];
var searchImages = await getSearchImages(tags: tags, page: page);
//adding images to List
return listOfImages;
}
It's something with FutureBuilder actually. I should've mention this.
Upd. "Fixed" with // ignore: argument_type_not_assignable
Looks like a problem with Dart Analysis for now
Upd. Error
It actually is an error which is pretty self explanatory.
The acutal error comes because of null safety in dart.
For ex:
void main(){
var number = getNumber(true);
int parsedNumber = int.parse(number);
print(parsedNumber);
}
String? getNumber(boolean value) {
if (value){
return null;
} else return "1";
}
So here, getNumber function either returns null or "1" depending upon the value of value variable. So, number variable's type is String?.
But the error shall arise in the next line when you try to call int.parse(). int.parse function takes an argument which should be a String but the value passed in the function is of type String?. So if we pass null in int.parse it shall throw an error.
That's why Dart analysis makes it easier to identify such cases by telling us that the value can be null and it might throw.
However the code depends upon your actual code of your project. It says that you are passing Future<List<GalleryPictureInfo>>? which is of nullable type to a function which requires Future<List<GalleryPictureInfo>>. So, before passing the value you might want to check if the value you are passing is not null.
If you are sure that the value can never be null then if for ex: if you are passing a variable called value, you might wanna try someFunctionWhereYouPassValue(value!)
That ! means that you are sure that the value will never be null.
For more details about null safety you can see:
https://dart.dev/null-safety/understanding-null-safety

Method 'replaceFirst' cannot be called on 'String?'

Error: Method 'replaceFirst' cannot be called on 'String?' because it is potentially null.
Try calling using ?. instead.)
.replaceFirst(r'$selectedRowCount', formatDecimal(selectedRowCount));
As Saffron-codes says, you cant do a replaceFirst on a 'String?' variable since it can be null, and dart is null safe.
There's two options for you to do. You can either make the variable a 'String' instead, if you do this you'll have to give it a value when initiating it:
String variableName = ''
Instead of:
String? variableName
You could also do a null-check (adding a questionmark before .replaceFirst) when calling replaceFirst, this is what it suggests doing in the error message 'Try calling using ?. instead.':
variableName?.replaceFirst(r'$selectedRowCount', formatDecimal(selectedRowCount));
Add the ! operator before . to make it non-nullable
!.replaceFirst(r'$selectedRowCount', formatDecimal(selectedRowCount));

How to take integer input in dart after the null safety update without using the if block for veriying weather the variable is null or not? [duplicate]

This question already has answers here:
"The argument type 'String?' can't be assigned to the parameter type 'String'" when using stdin.readLineSync()
(3 answers)
Closed 1 year ago.
Every time i need to input certain variable as integer i used to use this below code for that
import 'dart:io';
void main(List<String> arguments) {
int a = 0;
print("Enter a :");
String? x = stdin.readLineSync();
if (x != null) {
a = int.parse(x);
}
}
which is very hectic...very since the null safety was added from Dart 2.12 version.Before it the integer was inputted using this code int n = int.parse(stdin.readLineSync());
can anyone propose to make it smaller...since its hefty..
Since we are using Null Safety by default with Dart 2.12.2.
We also know that int.parse() take a String that cannot be null due to Null Safety.
however int.parse(readLineSync()) can return a int? which is a nullable value by simply adding the The null assertion operator (!) at the end of stdin.readLineSync() as show in code below
import 'dart:io';
void main(List<String> arguments) {
print("Enter a :");
int? a = int.parse(stdin.readLineSync()!);
}
The null assertion operator (!)
If you’re sure that an expression with a nullable type isn’t null, you can use a null assertion operator (!) to make Dart treat it as non-nullable. By adding ! just after the expression, you tell Dart that the value won’t be null, and that it’s safe to assign it to a non-nullable variable.
note:-If you’re wrong, Dart throws an exception at run-time. This makes the ! operator unsafe, so don’t use it unless you’re very sure that the expression isn’t null.
Hope you find this useful...(☞゚ヮ゚)☞

The argument type 'String?' can't be assigned to the parameter type 'String'

when I upgrade my flutter to 2.0.1, shows this error:
The argument type 'String?' can't be assigned to the parameter type 'String'.
this is my code:
enum SubStatus {
SUB,
UNSUB,
}
extension ResponseStatusExtension on SubStatus{
static const statusCodes = {
SubStatus.SUB: "sub",
SubStatus.UNSUB: "unsub",
};
String? get statusCode => statusCodes[this];
}
This is how to use it:
String url = "/post/sub/source/" + subStatus.statusCode + "/" + channelId;
this is the error UI:
what should I do to fix it? I tried to return String but in the enum code tell me should return String?:
what should I do?
Change the return type of statusCode to String and provide a default value.
String get statusCode => statusCodes[this] ?? '';
When accessing a map, there is a chance that you will get a null return value if the key does not exist in the map. Simply providing a default value will allow this code to compile. That default value should never be used unless you add something to the enum without adding a value to the map as well.
Edit:
After the comment from #Christopher Moore, I realized my mistake. So, I am going to directly use his solution over here as it is the correct one.
This is because of the new null-safety feature of Dart.
You will need to make the following change in the code and it will work:
String get statusCode => statusCodes[this] ?? '';
With new null-safety rules, the following data-type? x, the data type is followed by a question mark, means that the value x can be null. However, without the '?', it means that data-type x, it cannot be null.
So, basically String and String? are two different data types. That is why you get the error.
You can learn more here.
restart analysis server
add !
like this
subStatus.statusCode!