skip to Main Content

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


  1. Try with null check:

    style: Theme.of(context).textTheme.headlineSmall!.copyWith(color: Colors.white),
    
    Login or Signup to reply.
  2. An Addition to the above answer try using ‘?’

    style: Theme.of(context).textTheme.headlineSmall?.copyWith(color: Colors.white),
    
    Login or Signup to reply.
  3. Try with code I modified for you (I am also begenner with ethics)

    class TextWidget extends StatefulWidget {
      const TextWidget({Key? key}) : super(key: key);
    
      @override
      _TextWidgetState createState() => _TextWidgetState();
    }
    
    class _TextWidgetState extends State<TextWidget> {
      final _textController = TextEditingController();
      late final FocusNode _focusNode;
      TextSelection? _textSelection;
    
      @override
      void initState() {
        super.initState();
        _focusNode = FocusNode();
        _textController.text = '-> No issue if I remove SelectionContainer.disabled <-';
      }
    
      @override
      void dispose() {
        _focusNode.dispose();
        _textController.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        final textSpans = <InlineSpan>[
          const TextSpan(text: 'Hey 123 456 789'),
          WidgetSpan(
            child: SelectionContainer(
              child: TextField(
                controller: _textController,
                focusNode: _focusNode,
                style: Theme.of(context)
                    .textTheme
                    .bodyMedium!
                    .copyWith(color: Colors.transparent),
                decoration: InputDecoration(
                  border: InputBorder.none,
                  isDense: true,
                ),
                onTap: () {
                  if (_textSelection != null) {
                    _textSelection = null;
                    setState(() {});
                  }
                },
                onSelectionChanged: (textSelection, _) {
                  _textSelection = textSelection;
                },
              ),
            ),
          ),
        ];
    
        if (_textSelection != null) {
          _focusNode.requestFocus();
          _textController.selection = _textSelection!;
        }
    
        return Text.rich(TextSpan(children: textSpans));
      }
    }
    

    The TextWidget keeps track of the selected text by storing the TextSelection object in the _textSelection variable.
    The TextField calls the onSelectionChanged callback every time the text selection changes, and the TextWidget updates the _textSelection variable accordingly.

    In the build() method, the TextWidget checks if there is a selected text and sets the selection on the TextField widget using the TextEditingController.selection property.

    The TextField is focused using the FocusNode.requestFocus() method, and the _textSelection variable is set to null to clear the selection when the TextField is tapped.

    Login or Signup to reply.
  4. I’d like to address 2 issues here (possible solutions too).

    1. Theme

    That usually happens when you haven’t explicitly provided theme / darkTheme in MaterialApp.

    You can optionally provide Theme() widget wrapped around the Text widget you have in your code right now. Also make sure you have a Scaffold. Remember this theme would be scoped to the Text widget only.

        Theme(
          data: ThemeData(
            textTheme: const TextTheme(
                  /// define the ones you will invoke 
                   headlineSmall: TextStyle(
                      color: Colors.white,
                       )
                    ),
                ),
           child: Text(
                    'Hi Yoikers!',
                      style: Theme.of(context).textTheme.headlineSmall,
                    ),
          ),
    
    1. Null aware

    To avoid getting run-time errors add ? before invoking copyWith() 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!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search