1

I want this program to do three things. One, display the current value of my number. Two, show how much I have previously entered. Three, enter how much you want to subtract from the original value of the number.

But I am having one problem with my code. In the SubtractState class, I have an onPressed function for my FlatButton. Inside there is a variable called enteredValue. When I try to call this variable in the class 'Type' it's obviously giving me errors.

I know that I need to figure out a way to call a variable from a different class. But how should I do it?

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
            child: Center(
                child: GestureDetector(
      onTap: () {
        Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => Type()),
        );
      },
      child: Text(
        value.toString(),
        style: TextStyle(
          fontSize: 50.0,
        ),
      ),
    ))));
  }
}

class Type extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        home: Scaffold(
            body: Container(
                child: Center(
                    child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
              GestureDetector(
                child: Text("You Entered ${globals.enteredValue} previously"),
                onTap: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => Subtract()),
                  );
                },
              )
            ])))));
  }
}

class Subtract extends StatefulWidget {
  @override
  SubtractState createState() => SubtractState();
}

class SubtractState extends State<Subtract> {
  final _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            appBar: AppBar(
              elevation: 0,
              leading: IconButton(
                  color: Colors.black,
                  icon: Icon(Icons.arrow_back_ios),
                  onPressed: () => Navigator.pop(context)),
            ),
            body: Container(
                padding: const EdgeInsets.all(30.0),
                child: Container(
                  child: Center(
                      child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                        TextFormField(
                            textInputAction: TextInputAction.done,
                            controller: _controller,
                            keyboardType: TextInputType.number),
                        FlatButton(
                          child: Text("Subtract"),
                          onPressed: () {
                            //check if we can parse it
                            if (int.tryParse(_controller.text) == null)
                              return; //can't parse it
                            globals.enteredValue = int.parse(_controller.text);
                            setState(() {
                              value -= globals.enteredValue;
                            });
                            Navigator.push(
                              context,
                              MaterialPageRoute(builder: (context) => MyApp()),
                            );
                          },
                        ),
                      ])),
                ))));
  }
}

int value = 0;

1 Answer 1

2

I suggest one of the existing easy solutions to use a global library to share your application variables between classes :
Create a dart file named globals.dart

globals.dart :

library YOUR_PACKAGE_NAME.globals;
int x = 1 ; 

class1 :

 import 'package:YOUR_PACKAGE_NAME/globals.dart' as globals;
 print(globals.x) ; //prints 1 
 globals.x= 2 ; 

in your class2 :

import 'package:YOUR_PACKAGE_NAME/globals.dart' as globals;
print(globals.x) ; //prints 2 
Sign up to request clarification or add additional context in comments.

1 Comment

You are a star.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.