skip to Main Content

when I’m trying to define colour in dart code final Color _color; TextSection (this._color);
it gives me an error. do anyone know how to fix this error.

enter image description here

import 'package:flutter/material.dart';

 class Textsection extends StatelessWidget {

   final Color _color;

  TextSection (this._color);

  @override

  Widget build(BuildContext context) {
   return Container(
       decoration: BoxDecoration(
          color:_color,
        ),
        child:Text('hi')

    );
  }

 /* @override
  noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);*/
}

2

Answers


  1. Your class name is Textsection where section is lowercase but your constructor is named TextSection with an uppercase Section.
    Try renaming your class to TextSection so it matches your constructor name.

    Login or Signup to reply.
  2. You have to define the class constructor first then you are able to use this, I hope this will work for you.

    import ‘package:flutter/material.dart’;

    class Textsection extends StatelessWidget {
    
      final Color? color;
      const Textsection({Key? key, this.color}) : super(key: key);
    
     
    
      @override
    
      Widget build(BuildContext context) {
        return Container(
            decoration: BoxDecoration(
              color:color,
            ),
            child:Text('hi')
    
        );
      }
    
    /* @override
      noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);*/
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search