Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – How to get a variable of .dart file to another .dart file in flutter

Posted on November 14, 2022

Solution 1 :

You can do that in two ways

  1. Use Provider package from pub.dev, and then you can use the variable in any other dart file. This is the preferred way for somewhat complex program.
  2. You can just declare and initialize the variable outside the widget and than you can just use the variable in another dart file too.
    Like this,
String input = "";

class widget_model extends StatelessWidget {
final text;
widget_model(this.text);

@override
Widget build(BuildContext context) {
return Expanded(
  child: Row(
    children: <Widget>[
      Expanded(
        child: Column(
          children: <Widget>[
Container(
              height: 60,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                  gradient: LinearGradient(
                      colors: [Colors.pink, Colors.purpleAccent])),

              child: InkWell(
                onTap: () {
                  print("Pressed one");
                  print("The text is $input");
                  input=input+"1";
                  print("The text after $input");
                  Navigator.push(context, MaterialPageRoute(builder: (BuildContext)=>Homepage(input)));
                },

                child: Text(
                  "$text",
                  style: TextStyle(color: Colors.white, fontSize: 50),
                ),
              ),
            ),
          ],
        ),
      ),
      SizedBox(
        height: 10,
        width: 5,
      )
    ],
  ),
);

Solution 2 :

For a clean approach, you need a state management library like provider to share the variable between two widgets if both widget are in a different branches in the tree. If the other widget that used the same variable is a child of the current one, you can use stateful widget and setState everytime the input value changes.

Problem :

class widget_model extends StatelessWidget {
    final text;
    widget_model(this.text);

    String input = "";
    @override
    Widget build(BuildContext context) {
    return Expanded(
      child: Row(
        children: <Widget>[
          Expanded(
            child: Column(
              children: <Widget>[
    Container(
                  height: 60,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                      gradient: LinearGradient(
                          colors: [Colors.pink, Colors.purpleAccent])),

                  child: InkWell(
                    onTap: () {
                      print("Pressed one");
                      print("The text is $input");
                      input=input+"1";
                      print("The text after $input");
                      Navigator.push(context, MaterialPageRoute(builder: (BuildContext)=>Homepage(input)));
                    },

                    child: Text(
                      "$text",
                      style: TextStyle(color: Colors.white, fontSize: 50),
                    ),
                  ),
                ),
              ],
            ),
          ),
          SizedBox(
            height: 10,
            width: 5,
          )
        ],
      ),
    );
  }
}

The variable ‘input’ in this .dart file has been declared within the stateless widget but how can I get this variable to another .dart file. How to be notified on change of the variable value.

READ  [ANSWERED] android - Can't add google api key for Chromium build
Powered by Inline Related Posts

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress