0

Image of codeI am having a problem getting the value of a string into another string. I am passing a text value from one screen to another and I can display the passed it into a final string and show it in a widget. I want to further use this value, but I cannot set this value to a string variable.

final String text;
ParcelDetail({Key key, @required this.text}) : super(key: key);
String ref = text; //error in this line
Riven
  • 375
  • 2
  • 11
  • Check [this answer](https://stackoverflow.com/questions/67408949/why-does-this-error-occur-the-instance-member-cant-be-accessed-in-an-initi/67409176#67409176) to understand why this happens, and how to fix it. – croxx5f Jul 14 '21 at 18:08
  • Try initializing `ref` inside a methods like `build` or `initState` – Riven Jul 16 '21 at 04:48

2 Answers2

0

Add late and final to String ref = text

late final String ref = text;

Every variable inside a Stateless has to be final

Lucas Josino
  • 820
  • 1
  • 4
  • 14
  • Here works fine [DartPad example](https://dartpad.dev/?null_safety=true&id=1a28bdd9203250d3226cc25d512579ec) – Lucas Josino Jul 14 '21 at 17:40
  • > your example uses a widget to show the string and I am getting the same result when I use the widget but I cannot set title to a string variable – Erfan Fazal Jul 14 '21 at 17:58
0

Used the String "text" directly as a widget {"$widget.text"} into my function and resolved it.