I’m trying to make an android application using flutter from the tutorial, and when I want to change the color of the text according to the tutorial by means of :
children: [
text(
'Hi Yoikers!',
style: Theme.of(context).textTheme.headlineSmall.copyWith(color: Colors.white),
),
],
The program error :
The method 'copyWith' can't be unconditionally invoked because the receiver can be 'null'.
Try making the call conditional (using '?.') or adding a null check to the target ('!').
Can you guys help me with this code, thanks.
4
Answers
Try with null check:
An Addition to the above answer try using ‘?’
Try with code I modified for you (I am also begenner with ethics)
The
TextWidget
keeps track of the selected text by storing theTextSelection
object in the_textSelection
variable.The
TextField
calls theonSelectionChanged
callback every time the text selection changes, and theTextWidget
updates the_textSelection
variable accordingly.In the
build()
method, theTextWidget
checks if there is a selected text and sets the selection on theTextField
widget using theTextEditingController
.selection property.The
TextField
is focused using theFocusNode.requestFocus()
method, and the_textSelection
variable is set tonull
to clear the selection when theTextField
is tapped.I’d like to address 2 issues here (possible solutions too).
That usually happens when you haven’t explicitly provided
theme
/darkTheme
inMaterialApp
.You can optionally provide
Theme()
widget wrapped around theText
widget you have in your code right now. Also make sure you have aScaffold
. Remember this theme would be scoped to theText
widget only.To avoid getting run-time errors add
?
before invokingcopyWith()
as suggested by an other answer (and Flutter in console). This would mean it copies your specified values only if there exists a definition to copy with. In other words it would ignore your specs and use defaults without throwing errors.For more clarifications, provide more code, and add a comment!