'FLUTTER Error: Method 'copyWith' cannot be called on 'TextStyle?' because it is potentially null
Wow I have a whole lot of these:
Error: Method 'copyWith' cannot be called on 'TextStyle?' because it is potentially null.
- 'TextStyle' is from 'package:flutter/src/painting/text_style.dart' ('../flutter/packages/flutter/lib/src/painting/text_style.dart'). Try calling using ?. instead.
I don't understand the instruction, I've tried naively appending ? marks in various places, tried changing 'copyWith' to 'apply'.
This is the errourous code:
style: Theme.of(context)
.textTheme
.bodyText2
.copyWith(fontWeight: FontWeight.bold)
Uh, I don't get it. Major project changes for this null thing, and a fair amount of the time I feel I'm guessing. My dart files are now littered with !, which seems surprisingly verbose.
Anyway, my project won't compile without this, and I have a good few so help is appreciated.
Solution 1:[1]
You have to mark your bodyText2 value as not null with !, but be sure that the bodyText2 value returned is not null, otherwise you will get another error. To read more about null safety: null safety
Theme.of(context).textTheme.bodyText2!.copyWith(fontWeight: FontWeight.bold);
Solution 2:[2]
flutter pub upgrade fwfh_text_style
solved for me.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Eimantas G |
Solution 2 | LucasMW |