0

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.

Bavan
  • 31
  • 6

1 Answers1

0

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);
}
Jordi
  • 127
  • 1
  • 4