How to set final variable with setterHow can I disable setter generation without using final?Keeping final fields on the Widget or the State?Setting environment variables in FlutterVariable can't be used as setter because it is final (enum)How to offset a scaffold widget in Flutter?What is the purpose of assigning `const` value to a `final` variable in dart?Flutter : Bad state: Stream has already been listened toWhen is it nesessary to use final for variables in flutter (dart)What is the significance of the settings variable in MaterialPageRoute?The setter 'msg=' was called on null

Why would one crossvalidate the random state number?

Does running exec do anything?

Is the book wrong about the Nyquist Sampling Criterion?

How to remap repeating commands i.e. <number><command>?

Gerrymandering Puzzle - Rig the Election

Can my 2 children, aged 10 and 12, who are US citizens, travel to the USA on expired American passports?

Where are the "shires" in the UK?

Motion-trail-like lines

Would a "Permanence" spell in 5e be overpowered?

Page count conversion from single to double-space for submissions

Why would a military not separate its forces into different branches?

Is there an age requirement to play in Adventurers League?

Is any special diet an effective treatment of autism?

Is there precedent or are there procedures for a US president refusing to concede to an electoral defeat?

Simple Derivative Proof?

How to preserve a rare version of a book?

How did the Apollo guidance computer handle parity bit errors?

Endgame puzzle: How to avoid stalemate and win?

Is space itself expanding or is it just momentum from the big bang carrying things apart?

Counting the Number of Real Roots of A Polynomial

Clarification of algebra in moment generating functions

How to properly store the current value of int variable into a token list?

All of my Firefox add-ons been disabled suddenly, how can I re-enable them?

How can Internet speed be 10 times slower without a router than when using the same connection with a router?



How to set final variable with setter


How can I disable setter generation without using final?Keeping final fields on the Widget or the State?Setting environment variables in FlutterVariable can't be used as setter because it is final (enum)How to offset a scaffold widget in Flutter?What is the purpose of assigning `const` value to a `final` variable in dart?Flutter : Bad state: Stream has already been listened toWhen is it nesessary to use final for variables in flutter (dart)What is the significance of the settings variable in MaterialPageRoute?The setter 'msg=' was called on null






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















So I'm new to Dart and Flutter and im trying to set the color of a material depending on a specific string value passed in using a switch statement like so



class AnswerButton extends StatelessWidget

final String _option;
final String _answer;
final VoidCallback _onTap;
final Color _color = null;

set _color(_option)
switch (_option)
case "A":
_color = Colors.greenAccent;
break;

case 'B':
_color = Colors.redAccent;
break;

case 'C':
_color = Colors.blueAccent;
break;

case 'D':
_color = Colors.yellowAccent;
break;



AnswerButton(this._option, this._answer, this._onTap);

@override
Widget build(BuildContext context)
return new Expanded(
child: new Material(
color: _color,
child: new InkWell(
onTap: () => _onTap(),
child: new Center(
child: new Container(
decoration: new BoxDecoration(
border: new Border.all(color: Colors.white, width: 5.0)
),
padding: new EdgeInsets.all(20.0),
child: new Text(_answer.toString(),
style: new TextStyle(color: Colors.white, fontSize: 40.0, fontStyle: FontStyle.italic),),
),
),
),
)
);


)


but I am running into trouble as I'm not sure how to call the setter method to update _colour and the material is still white










share|improve this question
























  • you should call setState() method

    – Rubens Melo
    Mar 23 at 3:39











  • Thanks for the reply I am doing this when I am creating an object I am not trying to update the color at runtime

    – Mitch Q
    Mar 23 at 4:39

















0















So I'm new to Dart and Flutter and im trying to set the color of a material depending on a specific string value passed in using a switch statement like so



class AnswerButton extends StatelessWidget

final String _option;
final String _answer;
final VoidCallback _onTap;
final Color _color = null;

set _color(_option)
switch (_option)
case "A":
_color = Colors.greenAccent;
break;

case 'B':
_color = Colors.redAccent;
break;

case 'C':
_color = Colors.blueAccent;
break;

case 'D':
_color = Colors.yellowAccent;
break;



AnswerButton(this._option, this._answer, this._onTap);

@override
Widget build(BuildContext context)
return new Expanded(
child: new Material(
color: _color,
child: new InkWell(
onTap: () => _onTap(),
child: new Center(
child: new Container(
decoration: new BoxDecoration(
border: new Border.all(color: Colors.white, width: 5.0)
),
padding: new EdgeInsets.all(20.0),
child: new Text(_answer.toString(),
style: new TextStyle(color: Colors.white, fontSize: 40.0, fontStyle: FontStyle.italic),),
),
),
),
)
);


)


but I am running into trouble as I'm not sure how to call the setter method to update _colour and the material is still white










share|improve this question
























  • you should call setState() method

    – Rubens Melo
    Mar 23 at 3:39











  • Thanks for the reply I am doing this when I am creating an object I am not trying to update the color at runtime

    – Mitch Q
    Mar 23 at 4:39













0












0








0








So I'm new to Dart and Flutter and im trying to set the color of a material depending on a specific string value passed in using a switch statement like so



class AnswerButton extends StatelessWidget

final String _option;
final String _answer;
final VoidCallback _onTap;
final Color _color = null;

set _color(_option)
switch (_option)
case "A":
_color = Colors.greenAccent;
break;

case 'B':
_color = Colors.redAccent;
break;

case 'C':
_color = Colors.blueAccent;
break;

case 'D':
_color = Colors.yellowAccent;
break;



AnswerButton(this._option, this._answer, this._onTap);

@override
Widget build(BuildContext context)
return new Expanded(
child: new Material(
color: _color,
child: new InkWell(
onTap: () => _onTap(),
child: new Center(
child: new Container(
decoration: new BoxDecoration(
border: new Border.all(color: Colors.white, width: 5.0)
),
padding: new EdgeInsets.all(20.0),
child: new Text(_answer.toString(),
style: new TextStyle(color: Colors.white, fontSize: 40.0, fontStyle: FontStyle.italic),),
),
),
),
)
);


)


but I am running into trouble as I'm not sure how to call the setter method to update _colour and the material is still white










share|improve this question
















So I'm new to Dart and Flutter and im trying to set the color of a material depending on a specific string value passed in using a switch statement like so



class AnswerButton extends StatelessWidget

final String _option;
final String _answer;
final VoidCallback _onTap;
final Color _color = null;

set _color(_option)
switch (_option)
case "A":
_color = Colors.greenAccent;
break;

case 'B':
_color = Colors.redAccent;
break;

case 'C':
_color = Colors.blueAccent;
break;

case 'D':
_color = Colors.yellowAccent;
break;



AnswerButton(this._option, this._answer, this._onTap);

@override
Widget build(BuildContext context)
return new Expanded(
child: new Material(
color: _color,
child: new InkWell(
onTap: () => _onTap(),
child: new Center(
child: new Container(
decoration: new BoxDecoration(
border: new Border.all(color: Colors.white, width: 5.0)
),
padding: new EdgeInsets.all(20.0),
child: new Text(_answer.toString(),
style: new TextStyle(color: Colors.white, fontSize: 40.0, fontStyle: FontStyle.italic),),
),
),
),
)
);


)


but I am running into trouble as I'm not sure how to call the setter method to update _colour and the material is still white







dart flutter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 23 at 4:29







Mitch Q

















asked Mar 23 at 2:25









Mitch QMitch Q

14




14












  • you should call setState() method

    – Rubens Melo
    Mar 23 at 3:39











  • Thanks for the reply I am doing this when I am creating an object I am not trying to update the color at runtime

    – Mitch Q
    Mar 23 at 4:39

















  • you should call setState() method

    – Rubens Melo
    Mar 23 at 3:39











  • Thanks for the reply I am doing this when I am creating an object I am not trying to update the color at runtime

    – Mitch Q
    Mar 23 at 4:39
















you should call setState() method

– Rubens Melo
Mar 23 at 3:39





you should call setState() method

– Rubens Melo
Mar 23 at 3:39













Thanks for the reply I am doing this when I am creating an object I am not trying to update the color at runtime

– Mitch Q
Mar 23 at 4:39





Thanks for the reply I am doing this when I am creating an object I am not trying to update the color at runtime

– Mitch Q
Mar 23 at 4:39












1 Answer
1






active

oldest

votes


















0














First there's a small typo you have to fix.Your are trying to set the value of color instead of your final variable _color according to the official dart documentation:



 set color(_option)



To:



 set _color(_option)



Another thing is Material widget color won't be shown unless there's a child defined, you'll notice it when you use:



 new Material(
color: _color,
child: Text(''), // or any other widget you want to implement
)





share|improve this answer























  • Thanks for the reply, I fixed my copy paste error thanks for that, also I edited my question to include the complete file I omitted what I didn't think was pertinent. I have defined a child in the material, I am just not sure how to call the set method so that when i assign _color to the material it is green for example

    – Mitch Q
    Mar 23 at 4:37












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55310023%2fhow-to-set-final-variable-with-setter%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














First there's a small typo you have to fix.Your are trying to set the value of color instead of your final variable _color according to the official dart documentation:



 set color(_option)



To:



 set _color(_option)



Another thing is Material widget color won't be shown unless there's a child defined, you'll notice it when you use:



 new Material(
color: _color,
child: Text(''), // or any other widget you want to implement
)





share|improve this answer























  • Thanks for the reply, I fixed my copy paste error thanks for that, also I edited my question to include the complete file I omitted what I didn't think was pertinent. I have defined a child in the material, I am just not sure how to call the set method so that when i assign _color to the material it is green for example

    – Mitch Q
    Mar 23 at 4:37
















0














First there's a small typo you have to fix.Your are trying to set the value of color instead of your final variable _color according to the official dart documentation:



 set color(_option)



To:



 set _color(_option)



Another thing is Material widget color won't be shown unless there's a child defined, you'll notice it when you use:



 new Material(
color: _color,
child: Text(''), // or any other widget you want to implement
)





share|improve this answer























  • Thanks for the reply, I fixed my copy paste error thanks for that, also I edited my question to include the complete file I omitted what I didn't think was pertinent. I have defined a child in the material, I am just not sure how to call the set method so that when i assign _color to the material it is green for example

    – Mitch Q
    Mar 23 at 4:37














0












0








0







First there's a small typo you have to fix.Your are trying to set the value of color instead of your final variable _color according to the official dart documentation:



 set color(_option)



To:



 set _color(_option)



Another thing is Material widget color won't be shown unless there's a child defined, you'll notice it when you use:



 new Material(
color: _color,
child: Text(''), // or any other widget you want to implement
)





share|improve this answer













First there's a small typo you have to fix.Your are trying to set the value of color instead of your final variable _color according to the official dart documentation:



 set color(_option)



To:



 set _color(_option)



Another thing is Material widget color won't be shown unless there's a child defined, you'll notice it when you use:



 new Material(
color: _color,
child: Text(''), // or any other widget you want to implement
)






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 23 at 3:50









Mazin IbrahimMazin Ibrahim

1,5802817




1,5802817












  • Thanks for the reply, I fixed my copy paste error thanks for that, also I edited my question to include the complete file I omitted what I didn't think was pertinent. I have defined a child in the material, I am just not sure how to call the set method so that when i assign _color to the material it is green for example

    – Mitch Q
    Mar 23 at 4:37


















  • Thanks for the reply, I fixed my copy paste error thanks for that, also I edited my question to include the complete file I omitted what I didn't think was pertinent. I have defined a child in the material, I am just not sure how to call the set method so that when i assign _color to the material it is green for example

    – Mitch Q
    Mar 23 at 4:37

















Thanks for the reply, I fixed my copy paste error thanks for that, also I edited my question to include the complete file I omitted what I didn't think was pertinent. I have defined a child in the material, I am just not sure how to call the set method so that when i assign _color to the material it is green for example

– Mitch Q
Mar 23 at 4:37






Thanks for the reply, I fixed my copy paste error thanks for that, also I edited my question to include the complete file I omitted what I didn't think was pertinent. I have defined a child in the material, I am just not sure how to call the set method so that when i assign _color to the material it is green for example

– Mitch Q
Mar 23 at 4:37




















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55310023%2fhow-to-set-final-variable-with-setter%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript