6

I need to use datosusuario variable in another file how can I do this?

My main.dart is a Login I need use the query result variable to print that result in my homepage Widget

Main.dart

class LoginStates extends State<Login>{

@override

Widget build(BuildContext context) {

TextEditingController user=new TextEditingController();

TextEditingController pass=new TextEditingController();

Future<List> _login() async{

  final response =await 

http.post("https://exclinico.com/admin/FlutterClases/login.php", body: {

 "correo": user.text,

 "password": pass.text,

});

print(response.body);

var datosusuario = json.decode(response.body);


 if (datosusuario.length==0) {
  //print("$nombre");
   }
   else{
     //print("$nombre");
   }
}

I need to use datosusuario variable in the drawer info.

Home.dart

 return new Scaffold(
  appBar: new AppBar(
    centerTitle: true,
    title: new Image.asset("assets/LOGO.png"),
    backgroundColor: Colors.blue,
  ),
  drawer: new Drawer(
    child: new ListView(
      children: <Widget>[
        new UserAccountsDrawerHeader(
            accountName: new Text("Eduardo"),
            accountEmail: new Text("eduardo.hernandez@Utacapñulco.edu.mx"),
        currentAccountPicture: new CircleAvatar(
          backgroundColor: Colors.white,
          child: new Text("E"),
        ),),

3 Answers 3

9

Wonderful, you just discovered a fundamental computer science problem: state management!

There are a lot of ways to do state management in Flutter and I really recommend you check out the Flutter state management talk at this year's Google I/O, where they outline the pros and cons of the different ways of handling state in Flutter.

Just for the sake of it, here's one possible way of doing it pragmatically: You could just add the datosusuario as a constructor parameter to your Home widget, like so:

class Home extends StatelessWidget {
  Home({
    @required this.datasusuario,
  });

  final datasusuario;

  @override
  Widget build() {
    // here you can use the datasusuario
  }
}

and then pass it to the Home widget from the Login widget:

Home(datasusuario: datasusuario),
Sign up to request clarification or add additional context in comments.

Comments

7

An alternative way to create a global that I prefer to use is to create a Provider/Service for this.

For example, say you want your variable importable and available in any file without instantiating an object, you can create a class with a static value like so:

Service/Provider

class GlobalData {
  static const var datosusuario = datosusuario_value;
}

Use in another file

import 'package:my_app_name/path/to/provider.dart' as Globals;

Globals.datosusuario;

Comments

4

There are 2 ways, first you can pass the variable to the home.dart file using its constructor.

In your home.dart file,

class Home extends StatelessWidget {
  final datosusuario;

  Home(this.datosusuario);

  @override
  Widget build(BuildContext context) {
    ...
  }
}

In your main.dart file, use

Home(datosusuario)

Second solution: You can make the variable global to the main.dart file and import this file in home.dart and use it, but make sure it is not null.

var datosusuario; // in top of the main.dart

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.