Search code examples
flutterdartvariables

How to use a variable in other categories


I saved the value of 'result.address' received from the address api as a variable called final _resultAddress.

Assuming that you want to use this value in Text inside Child of Container, if you use Text(result.address), an error occurs. (undefiend name 'result.address)!

My question is simple. How can I use a variable in multiple places? (other methods, functions, etc.)


GestureDetector(
  onTap: ()async{
    await Navigator.push(context, MaterialPageRoute(
      builder: (_) => KpostalView(
        callback: (Kpostal result) {
          final _resultAddress = result.address;
          print(_resultAddress);
        },
      ),
    ));
  },
    child: Container(
      height: 48.0,
      decoration: BoxDecoration(
        color: Colors.red,
        border: Border.all(
          color: Colors.green,
        ),

      ),
      child: Text(_resultAddress),
    ),
  ),

I tried to name the variable final, const, static, but it didn't work.


Solution

  • declare it as global

    final _resultAddress;
    

    Then use setState

    callback: (Kpostal result) {
      setState(() {
       _resultAddress = result.address;
      });  
    },