0

I pass data from textcontroller via getter to text widget and I don't understand why text on widget pronounce doesn't appear. It seems to me that there is some logic in my code, but I'm new to flutter and maybe I don't understand something, tell me. code compiles

import 'package:flutter/material.dart'; void main() { runApp( MaterialApp( home: MyCustomForm(), ), ); } class MyCustomForm extends StatefulWidget { MyCustomForm({super.key}); String s = ''; String get ninja { return s; } @override State<MyCustomForm> createState() => _MyCustomFormState(); } class _MyCustomFormState extends State<MyCustomForm> { final myController = TextEditingController(); @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Pronounce(), TextField( controller: myController, ), ], ), ), floatingActionButton: FloatingActionButton( onPressed: () => setState (() {widget.s = myController.text;}), child: const Icon(Icons.update), ), ); } } class Pronounce extends StatefulWidget { Pronounce ({super.key}); @override State<Pronounce> createState() => _PronounceSt(); } class _PronounceSt extends State<Pronounce> { @override Widget build (context) { return Text (MyCustomForm().ninja); } } 
1

3 Answers 3

0

Problem is your getter is in MyCustomForm technically it not shared properly

You should pass data by create constructor and pass it to another widget or page like this and I will add some constrcutor tips

class Pronounce extends StatefulWidget { // Define Variable final String text; // then create constructor // ps. if your variable is nullable or has default value you can remove required Pronounce ({ super.key , required this.text, // ex. when you have default value // this.text = 'default text', // ex. nullable case // this.text, }); @override State<Pronounce> createState() => _PronounceSt(); } class _PronounceSt extends State<Pronounce> { @override Widget build (context) { return Text (text); // Nullable case you can do like this ?? after nullable variable mean when value is null use this ... // return Text (text ?? 'Text is empty'); } } 
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for u clue, probably this question is not so much about the language features as it is about code design, but sometimes it becomes extremely difficult to move your brain
you forgot a small detail which is 'widget.text', he will never be able to reach that using StatefulWidget.
0

The problem lies here when you are accessing MyCustomForm().ninja variable as it creates a new instance of variable s so when MyCustomForm class is initialised the value of s again becomes empty you can rewrite this code like this .

If you have any specific requirement of accessing the variable of the class directly then let me know we will figure out something for it also

import 'package:flutter/material.dart'; void main() { runApp( MaterialApp( home: MyCustomForm(), ), ); } class MyCustomForm extends StatefulWidget { const MyCustomForm({super.key}); @override State<MyCustomForm> createState() => _MyCustomFormState(); } class _MyCustomFormState extends State<MyCustomForm> { final myController = TextEditingController(); String s = ''; @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Pronounce(text: s), // 👈 Pass the value here TextField( controller: myController, ), ], ), ), floatingActionButton: FloatingActionButton( onPressed: () => setState(() { s = myController.text; }), child: const Icon(Icons.update), ), ); } } class Pronounce extends StatelessWidget { final String text; const Pronounce({super.key, required this.text}); @override Widget build(BuildContext context) { return Text(text); } } 

Comments

-1

you can do it like this

class Pronounce extends StatelessWidget { Pronounce ({super.key,required this.text}); final String text; @override Widget build (context) { return Text (MyCustomForm().ninja); } } 

and you can pass it like this

class MyCustomForm extends StatefulWidget { MyCustomForm({super.key}); @override State<MyCustomForm> createState() => _MyCustomFormState(); } class _MyCustomFormState extends State<MyCustomForm> { final myController = TextEditingController(); String s = ''; @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Pronounce(text: s), TextField( controller: myController, ), ], ), ), floatingActionButton: FloatingActionButton( onPressed: () => setState (() {s = myController.text;}), child: const Icon(Icons.update), ), ); } } 

1 Comment

thank you for u clue, probably this question is not so much about the language features as it is about code design, but sometimes it becomes extremely difficult to move your brain. so, thanks again

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.