Kashish Khullar
Kashish Khullar

Reputation: 424

'TextStyle?' can't be assigned to the parameter type 'TextStyle'

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?

Upvotes: 14

Views: 18787

Answers (5)

PrimoUomo89
PrimoUomo89

Reputation: 1

I know this is 2+ years old but I think the real (EDIT: Simpler, because previous answer works) answer is still missing. The issue is that it's looking for a non-null TextTheme and you're providing a nullable TextTheme. BUT the property itself is looking for nullable TextTheme. It's your line that is making it look for a non-null TextTheme

So in this line:

ButtonStyle(textStyle: MaterialStateProperty.all<TextStyle>(Theme.of(context).textTheme.button))

This property can accept a nullable TextStyle:

ButtonStyle( textStyle:

This expression returns a nullable TextStyle

Theme.of(context).textTheme.button)

It's your type declaration that is saying it can't be nullable:

MaterialStateProperty.all<TextStyle>

You just need to add a ? to your type:

MaterialStateProperty.all<TextStyle?>

Upvotes: 0

MaterialStateTextStyle.resolveWith((states) => TextStyle(fontSize: 12),

You can use .resolveWith Method to create a MaterialStateTextStyle from a MaterialPropertyResolver callback function.

Upvotes: 2

evanca
evanca

Reputation: 629

You can also get this error if you imported wrong library for the material TextStyle.

This may happen when you used:

import 'dart:ui';

Instead of:

import 'package:flutter/material.dart';

Upvotes: 24

Ayyaz Shair
Ayyaz Shair

Reputation: 620

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)

Upvotes: 1

Tasnuva Tavasum oshin
Tasnuva Tavasum oshin

Reputation: 4750

Initialize Your Style in the Buildcontext, Example :

 var _styleType = Theme.of(context)
                                .textTheme
                                .body1
                                .copyWith(color: Colors.white);

Then Apply It to the Widget :

Container(
                        margin: EdgeInsets.symmetric(vertical: 10),
                        child: new Text("\u00a9 TasnuvaOshin",
                            overflow: TextOverflow.ellipsis,
                            style: _styleType),
                      ),

Upvotes: 1

Related Questions