not able to complete the program [duplicate] - dart

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.
I'm new to dart and having a hard time figuring out things. so, I just need some help completing the program below. I have no idea where I'm going wrong.
All I'm getting is an error related to null safety
question :-
Write a program to obtain a number N and increment its value by 1 if the number is divisible by 4 otherwise decrement its value by 1.
import 'dart:io';
void main(){
String? input = stdin.readLineSync();
int number = int.parse(input);
}
This is all that came to my mind, I know the logic, but I'm stuck at getting the user input and converting it.

As the comment suggest, in this answer is explained how Dart will handle if stdin.readLineSync does give a null value. So this should work, notice the ! at the end of stdin.readLineSync.
import 'dart:io';
void main() {
var input = stdin.readLineSync()!;
var number = int.parse(input);
}

Related

Problem with getting user input , How do i solve this problem?

import 'dart:io';
void main (List<String> args){
print('Enter name');
String? name = stdin.readLineSync();
print (name);
print ("Enter first digit");
int?num1 = int.parse(stdin.readLinesync()!);
print ("Enter second digit");
int num2? = int. parse(stdin.readLineSync()!);
int result = num1 + num2;
print(result);
}
There is absolutely nothing wrong with the code. I'm sure of it.
Getting user input in the debug console in visual studio code is a bit cumbersome. I'm input a integer alright. But when I want to print. It doesn't work quite accurately.
f
For instance when input an integer in the console and i want to print "the number you entered is(the integer that was imputed in the console), on the integer displays but the string doesn't display.
I'm also finding it difficult perform any arithmetic calculations with the integers that i input in the debug console.
Also when I specify that the user input should be an integer, I'm able to input an integer and a string as well. This shouldn't be

How to read an integer input from the user input in dart [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.
I actually don't know how to take a integer input from the input console. Then I tried this after a little research.
My Code:
import 'dart:io';
void main() {
stdout.write("Enter a number: ");
int num1 = int.parse(stdin.readLineSync());
print(num1);
}
But it doesn't work, showing an error message,
ERROR: The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't.
Then finally I came to know that in dart2.12+ versions dart introduced null safety. Any suggestion to do it properly in the null safety environment.
The readLineSync() method returns a String?. The ? symbol indicates this variable may be null.
On the other hand, the int.parse() method expects a String, without the ? symbol. This means it doesn't know how to handle if the result from the readLine method comes null.
The easiest way to solve this is to give a default value in case the result comes null:
int num1 = int.parse(stdin.readLineSync() ?? '0');
The ?? operator makes the expression evaluates to the right side, if the left side is null. So giving it a default value it won't have to bother with a nullable type.
There are other operators you can use. You can read more about it in the Dart documentation about it.
Try this
import 'dart:io';
void main()
{
// Asking for favourite number
print("Enter your favourite number:");
// Scanning number
int n = int.parse(stdin.readLineSync());
// Printing that number
print("Your favourite number is $n");
}
If this not work I think you should try this answer link here

Null-aware operator when traversing a nested map [duplicate]

This question already has answers here:
Null-aware operator with Maps
(7 answers)
Closed 3 years ago.
How do you check for nulls when accessing second level map elements?
E.g.
var clientName = item['client']['name'];
will throw an exception if item does not contain client
I'd like something like
item['client']?['name']
or
item['client']?.['name']
But that won't compile.
Surely I can do item['client'] twice or introduce a local var for it but that feels subpar.
I think that this is similar to a question I asked some time ago.
Basically, this would be the solution for your case:
(item['client'] ?? const {})['name']
This makes use of the null-aware ?? operator which just returns an empty map in the case that 'client' is not present in item.
With putIfAbsent, you can use ? operator.
item.putIfAbsent("client", (){})
?.putIfAbsent("name", (){});
https://api.dartlang.org/stable/2.3.1/dart-core/Map/putIfAbsent.html

Set integer 'variable' using result of NSString [duplicate]

This question already has an answer here:
Swift: Converting a string into a variable name
(1 answer)
Closed 7 years ago.
I'm using a single action to handle 10 UISwitches by sending a tag value to it. I have a number of integers initialised when launched thus;
int switch_1 = 0;
int switch_2 = 0; etc
When a particular switch is switched on, I want to set the integer to the corresponding integer 'variable' with a 1
So, if switch 2 with tag 2 is turned on, it puts a '1' in the corresponding int 'switch_2' as an integer.
I am getting a string with the right name via 'stringWithFormat' by appending the tag value but don't know how to write the 1 to the corresponding integer variable from it.
Any help would be greatly appreciated. Essentially, I want to write an integer to a 'variable' name with the same name as the generated string value.
Thanks
You can use an array where each index represents your switch, so it would be like following:
int swtichArray[10] = {0}; // if you have 10 switches
While in your action where you handling action of multiple buttons, you could do that:
switchArray[tagOfSwitch] = 1; // if tags are staring from 0 onwards 9

Decimal values in tuple give garbage values in Swift [duplicate]

This question already has an answer here:
swift tuple has unexpected print result
(1 answer)
Closed 8 years ago.
Decimal values in tuple give garbage value
let qwerty = ("Rachit", 5.55)
println(qwerty)
It will give output (Rachit, 1.28416751252943e-313)
while
println(qwerty.1)
It will give output 5.55
Why?
Its a Bug . You Can Refer
This Link
Mainly we don't use whole tuple as you used.We used its Values using .0/.1 or using somename
Like
let http200Status = (statusCode: 200, description: "OK")
println("The status code is \(http200Status.statusCode)")
// prints "The status code is 200"
println("The status message is \(http200Status.description)")
// prints "The status message is OK”

Resources