I got this error from terminal, tried to make “PomodoroStatus” a variable changed to capitalize letter to “p”, but no luck, how to solve it?
(red warning)
lib/screens/home_screen.dart:71:40: Error: The argument type ‘String?’ can’t be assigned to the parameter type ‘String’ because ‘String?’ is nullable and ‘String’ isn’t.
statusDescription[PomodoroStatus],
this is my code:
import 'dart:async';
import 'package:startup_pomodoro/utils/constants.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:percent_indicator/circular_percent_indicator.dart';
import '../utils/constants.dart';
import '../widget/progress_icons.dart';
import '../widget/custom_button.dart';
import '../model/pomodoro_status.dart';
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
State<Home> createState() => _HomeState();
}
const _btnTextStart = 'Start Pomodoro';
const _btnTextResumePomodoro = 'Resume Pomodoro';
const _btnTextResumeBreak = 'Resume Break';
const _btnTextStartShortBreak = 'Take Short Break';
const _btnTextStartLongBreak = 'Take Long Break';
const _btnTextStartNewSet = 'Start New Set';
const _btnTextPause = 'Pause';
const _btnTextReset = 'Reset';
class _HomeState extends State<Home> {
int remainingTime = pomodoroTotalTime;
String mainBtnText = _btnTextStart;
PomodoroStatus pomodoroStatus = PomodoroStatus.pausedPomodoro;
late Timer _timer;
int pomodoroNum = 0;
int setNum = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xFFD95550),
body: SafeArea(
child: Center(
child: Column(children: [
SizedBox(height: 10,),
Text(“Pomodoro number:$pomodoroNum”,
style: TextStyle(fontSize: 32, color: Colors.white),
),
SizedBox(height: 10,),
Text(“Set: $setNum”,
style: TextStyle(fontSize: 32, color: Colors.white),
),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircularPercentIndicator(
backgroundColor: Color(0xFFF5EEEE),
radius: 150.0,
lineWidth: 15.0,
percent: 0.3,
circularStrokeCap: CircularStrokeCap.round,
center: Text(_secondsToFormatedString(remainingTime),
style: TextStyle(fontSize: 40, color: Colors.white),
),
progressColor: statusColor[pomodoroStatus],
),
SizedBox(height: 10,),
ProgressIcons(
total: pomodoroPerSet,
done: pomodoroNum - (setNum * pomodoroPerSet),
),
SizedBox(height: 10,),
Text(
statusDescription[PomodoroStatus],
style: TextStyle(color: Colors.white),
),
SizedBox(height: 10,),
CustomButton(
onTap: () {},
text: 'Start', ),
CustomButton(
onTap: () {},
text: 'Reset', )
],
),
),
],),
),
)
);
}
_secondsToFormatedString(int seconds) {
int roundedMinutes = seconds ~/60;
int remainingSeconds = seconds - (roundedMinutes * 60);
String remainingSecondsFormated;
if (remainingSeconds < 10) {
remainingSecondsFormated = '0$remainingSeconds';
} else {
remainingSecondsFormated = remainingSeconds.toString();
}
return '$roundedMinutes:$remainingSecondsFormated';
}
_mainButtonPressed() {
switch(pomodoroStatus) {
case PomodoroStatus.pausedPomodoro:
_startPomodoroCountDown();
break;
case PomodoroStatus.runningPomodoro:
// TODO: Handle this case.
break;
case PomodoroStatus.runningShortBreak:
// TODO: Handle this case.
break;
case PomodoroStatus.pausedShortBreak:
// TODO: Handle this case.
break;
case PomodoroStatus.runningLongBreak:
// TODO: Handle this case.
break;
case PomodoroStatus.pausedLongBreak:
// TODO: Handle this case.
break;
case PomodoroStatus.setFinished:
// TODO: Handle this case.
break;
}
}
_startPomodoroCountDown() {
pomodoroStatus = PomodoroStatus.runningPomodoro;
_cancelTimer();
_timer = Timer.periodic(Duration(seconds: 1), (timer) =>{
if (remainingTime > 0) {
setState((){
remainingTime --;
mainBtnText = _btnTextPause;
})
}
else {
//todo playSound(),
pomodoroNum ++,
_cancelTimer(),
if (pomodoroNum % pomodoroPerSet == 0) {
pomodoroStatus = PomodoroStatus.pausedLongBreak,
setState((){
remainingTime = lonBreakTime;
mainBtnText = _btnTextStartLongBreak;
}),
} else {
pomodoroStatus = PomodoroStatus.pausedShortBreak,
setState((){
remainingTime = shortBreakTime;
mainBtnText = _btnTextStartShortBreak;
}),
}
}
});
}
_cancelTimer(){
if (_timer != null) {
_timer.cancel();
}
}
}
2
Answers
The Text widget cannot be null. A string must first be given as a parameter.
71:40 –> Text(statusDescription[PomodoroStatus])
It does not accept because there is a possibility that the value may be empty on this line.
If you’re sure it’s not empty, add a (!) to the end and you can continue.
Text(statusDescription[PomodoroStatus]!)
Solution:
Change:
to this:
Explanation:
The
Text
widget expects a String and the result ofstatusDescription[pomodoroStatus]
is nullable i.e is of typeString?
.String?
is not equivalent toString
so you need to cast it to String by adding!
after the nullable variable.Note:
Only use this if you’re sure that
statusDescription[pomodoroStatus]
is not null.If you’re not sure, you can do a null-check and give a default variable if it is null.
Like this: