Search code examples
flutterdartgeolocation

implement if statement inside subtitle in ListTile


is there any way i can implement an if statement inside a subtitle in ListTile? i can put it directly inside a widget but not in title/subtitle

ListTile(
  title: Text("Location"),
  subtitle: if (_currentPosition != null) Text(_currentAddress),
  trailing: IconButton(...),
),

this gives an error "Expected an identifier. dart(missing_identifier)"


Solution

  • You have to do it like this:

    subtitle: _currentPosition == null?
            Text('No position found') //code if above statement is true
            :Text(_currentAddress), //code if above statement is false