Home > Mobile >  Error in dart code final Color _color; TextSection (this._color);
Error in dart code final Color _color; TextSection (this._color);

Time:01-12

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);*/
}

CodePudding user response:

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.

CodePudding user response:

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);*/
}
  •  Tags:  
  • Related