Home  >  Q&A  >  body text

Flutter cannot get the new value of a variable when called in another class

I have this StatefulWidget which is supposed to retrieve data from MySQL database based on conditions and add these retrieved values ​​to an array so that I can display on a ListView using the array values. My problem is that sometimes when the array (_fileValues) and the _custUsername variable are empty or not updated, the exception is not thrown.

This is my stateful widget (main.dart)

String? _custUsername;
List<String> _fileValues = <String>[];

class cakeLogin extends StatefulWidget {
  @override
  cakeLoginPageMain createState() => cakeLoginPageMain(); 
}

class cakeLoginPageMain extends State<cakeLogin> {

  String? _CustEmailInit; 
  BuildContext? loginContext;

  List<String> _fileNameStores = <String>[];

  void callData() {
    // @ Get username based on user email and assign it to _custUsername variable
    UsernameGetter().Connection(_CustEmailInit!).then((UsernameValue) {
      _custUsername = UsernameValue!;
    });
    
    // @ Get item names based on the user username (_custUsername) and add them into
    // _fileNameStores
    NameGetter().Connection(_custUsername).then((NamesValues) {
      for(var _nameItems in NamesValues) {
        setState(() {
          _fileNameStores.add(_nameItems);
        });
      }
    });
    // @ Remove duplicates value and add them into main list (_fileValues)
    var _removeDuplicates = _fileNameStores.toSet().toList();
    for(var item in _removeNamesDupes) {
      setState(() {
       _fileValues.add(item);
      });
     }

  }
  
  @override
  Widget build(BuildContext context) {

    final _emailController = TextEditingController();

    return Scaffold(
      appBar: AppBar(
        title: Text("Login to App",
          style: TextStyle(
            fontSize: 18,
            color: setupFontCol,
            fontWeight: FontWeight.bold
          )),
        backgroundColor: setupThemeBar,
        centerTitle: true,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(20),
        )
      ),
      backgroundColor: setupMainTheme,
      body: Padding(
        padding: EdgeInsets.all(20),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextFormField(
              controller: _emailController,
              style: TextStyle(color: Color.fromARGB(255, 214, 213, 213)),
              decoration: InputDecoration(
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(10),
                ),
                filled: true,
                hintStyle: TextStyle(color: Color.fromARGB(255, 197, 197, 197)),
                hintText: "Enter your email",
                fillColor: setupThemeBar
              ),
            ),

           SizedBox(height: 18),

           SizedBox(height: 25),

          SizedBox(
            height: 45,
            width: 500,
            child: ElevatedButton(
              style: 
                ElevatedButton.styleFrom(
                  primary: setupFontCol,
                  onPrimary: Colors.white,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10),
                  )
                ),
              onPressed: () {

                var _CustEmail = _emailController.text;

                if(_CustEmail.contains("@gmail.com") || _CustEmail.contains("@email.com")) {
                   if(_CustEmail != "") {
                          _CustEmailInit = _CustEmail;
                          loginContext = context;

                          _isFromLogin = true;
  
                          _fileValues.clear();
                          callData();

                    } else {
                      alertDialog("Please enter your email address",context);
                    }
                } else {
                    alertDialog("Email address is not valid",context);
                }
              },
              //color: setupFontCol,
              child: Text("Login",
                style: TextStyle(
                  color: Colors.white,
                  fontWeight: FontWeight.normal,
                  fontSize: 14,
                ))
              ),
            ),
          ],
        ),
      ),      
    );
  }
}

UsernameGetter Class (This class will retrieve the user username from the database table based on the email in the text field input)

When I print the _usernameDetected class from the UsernameGetter class it returns an updated version of the returned string, but when I assign _custUsername to When UsernameValue is removed from main.dart, it does not update and sometimes returns null value.

import 'package:mysql_client/mysql_client.dart';
import 'dart:convert';
import 'dart:ffi';
import 'dart:typed_data';
import 'package:signuppage/main.dart' as main;
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

class UsernameGetter {
  Future<String> Connection(String _custEmail) async {
    final conn = await MySQLConnection.createConnection(
      host: "",
      port: ,
      userName: "",
      password: "",
      databaseName: "",
    );

    await conn.connect();

    var _getUsernameQue = await conn.execute("SELECT CUST_USERNAME FROM information WHERE CUST_EMAIL = :email", 
    {
      "email": _custEmail
    });

    String? _usernameDetected;
    for(final _rowsOfEmail in _getUsernameQue.rows) {
      _usernameDetected = _rowsOfEmail.assoc()['CUST_USERNAME']!;
    }
    return await _usernameDetected!;
  }
}

NameGetter Class (Retrieve user item name stored in text column)

For the NameGetter class, the same thing happens with UsernameGetter. When I retrieve the value from main.dart and add the value to the _fileValues list, sometimes the value is empty and sometimes the value is not updated.

import 'package:mysql_client/mysql_client.dart';
import 'dart:convert';
import 'dart:ffi';
import 'dart:typed_data';
import 'package:signuppage/main.dart' as main;
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

class NameGetter {

  Future<List<String>> Connection(String _custUsername) async {
    final conn = await MySQLConnection.createConnection(
      host: "",
      port: ,
      userName: "",
      password: "",
      databaseName: "",
    );
    await conn.connect();

    var _getItemNames= await conn.execute("SELECT ITEM_NAMES FROM item_details WHERE CUST_USERNAME = :username",
      {
        "username": _custUsername
      });

    List<String>? _itemNamesValues= <String>[]; 
    for(var _rowsOfItemNames in _getItemNames.rows) {
      var _retrieveNames = await _rowsOfItemNames.assoc()['ITEM_NAMES']!;
      _itemNamesValues.add(_retrieveNames);
    }

    return await _itemNamesValues;
  }
}

I'm not sure where my mistakes are, but I'm pretty sure I'm making a lot of them and I need someone to help me figure them out. Especially regarding why lists and variables are not updated/sometimes empty.

Reference link:

Flutter ListView not updated

My list in flutter is not updated in the code

Why are variable values ​​not refreshed in flutter?

P粉670107661P粉670107661181 days ago345

reply all(1)I'll reply

  • P粉521697419

    P粉5216974192024-04-02 09:09:07

    question

    callData() The simplified version of the method is as follows:

      var foo;
    
      void callData() {
        someAsyncFunction().then( ... foo = value ... );
        anotherAsyncFunction(foo).then( ... );
      }
    

    As mentioned above, anotherAsyncFunction(foo) is called immediately after someAsyncFunction() and before then() > assignment The value of foo.

    You can observe this behavior yourself by strategically inserting print() statements.

    solution

    Under the hood, Future.then() and async/await are the same thing. However, it is often easier for people to infer the behavior of asynchronous code when using async/await because then it reads like sequentially executed code.

    While you can use any of these, I recommend moving to async/await to make the code easier to understand when reading.

    The following is an example of how to refactor callData():

      Future callData() async {
        // @ Get username based on user email and assign it to _custUsername variable
        var custUsername = await UsernameGetter().Connection(_CustEmailInit!);
        
        // @ Get item names based on the user username (_custUsername) and add them into
        // _fileNameStores
        var namesValues = await NameGetter().Connection(custUsername);
    
        setState(() {
          _fileNameStores.addAll(namesValues);
          // @ Remove duplicates value and add them into main list (_fileValues)
          _fileValues.addAll(_fileNameStores.toSet());
        });
      }
    

    Written like this, it is obvious that custUsername is assigned before calling NameGetter().Connection(custUsername).

    Please be sure to read throughAsynchronous programming: futures, async, await.

    reply
    0
  • Cancelreply