Issue
I am Getting this null safety error while working on my flutter app.
The argument type 'TextStyle?' can't be assigned to the parameter type 'TextStyle'.
Here is the code snippet that is throwing this error:
ButtonStyle(textStyle: MaterialStateProperty.all<TextStyle>(Theme.of(context).textTheme.button))
VS Code suggest this solution to put not operator at the end of the argument.
ButtonStyle(textStyle: MaterialStateProperty.all<TextStyle>(Theme.of(context).textTheme.button!))
Question: Is this a good practice to use ! in my code and what other solutions are possible in this case?
Solution
Material state properties represent values that depend on a widget's material "state". The state is encoded as a set of MaterialState values, like MaterialState.focused, MaterialState.hovered, MaterialState.pressed. For example the InkWell.overlayColor defines the color that fills the ink well when it's pressed (the "splash color"), focused, or hovered. The InkWell uses the overlay color's resolve method to compute the color for the ink well's current state.
You could use only Theme of context to use theme textStyle like this:
Text("copied text theme",
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.button)
Answered By - Ayyaz meo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.