Pinak Parate
Pinak Parate

Reputation: 131

How to transfer information from one class to another in Flutter?

I just wanted to know how to pass data from one class to another in Flutter. Like suppose, I want to transfer the text field data that is in the first class, and then pass it onto another class (which is in another file, but same folder) to display it. Please help...

Upvotes: 0

Views: 2416

Answers (2)

Inan Mahmud
Inan Mahmud

Reputation: 244

If you want to pass from one class to another you can do something like this,

Example: Here is the first class given below which will have a TextField

class ExampleClassOne extends StatelessWidget {
  final textEditingController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        child: Column(
          children: [
            TextField(
              controller: textEditingController,
            ),
            ElevatedButton(
                onPressed: () {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (_) => ExampleClassTwo(
                                text: textEditingController.text,
                              )));
                },
                child: Text("Enter")),
          ],
        ),
      ),
    );
  }
}

Here is the class which will receive the text value. It will receive using it's constructor.

class ExampleClassTwo extends StatelessWidget {
  final String text;

  const ExampleClassTwo({this.text});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        child: Text(text),
      ),
    );
  }
}

Upvotes: 0

altair
altair

Reputation: 141

Data is held in objects, not in classes, unless it is defined as "static". To transfer data, you can do it by methods, constructors, fields, etc.. For example:

class A {
  String data;

}

class B {
  String data;
  static String static_data;
}

somewhere in the code you have objects:

var a = A();
var b = B();

To tranfer the data you can use the public field directly:

b.data = a.data;

or in the static case:

B.data = a.data;

In Flutter, you typically have Widget with TextField. To access the text in it, use TextEditingController. Then open the other widget with constructor passing value:


class _Widget1State extends State<Widget1> {

  final text_ctrl = TextEditingController();

  Widget build(BuildContext context) {
    return Column(children: [
      TextField(controller: text_ctrl),
      ElevatedButton(onPressed: () => Navigator.pushReplacement(
          context, MaterialPageRoute(builder: (context) => Widget2(text_ctrl.text)));)
    ]);
  }
}

and the Widget2 will take it like this:

class Widget2 extends StatelessWidget {
  final String text;

  Widget2 (this.text);

  @override
  Widget build(BuildContext context) {
    return Text(text);
  }
}

Upvotes: 2

Related Questions