Home > Enterprise >  How to use a variable in other categories
How to use a variable in other categories

Time:11-14

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.

CodePudding user response:

declare it as global

final _resultAddress;

Then use setState

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

CodePudding user response:

Declare "_resultAddress" as a global variable and then Use it at any cate.

  • Related