flutterdartappbar

Is there a way to avoid using 'null' to show appbar actions with condition in Flutter?


I wonder if there is a way to avoid setting null to actions if i have this state condition:

appBar: AppBar(
      title: provider.appBarTitle,
      actions: provider.editState == EditState.inactive
          ? [
              IconButton(
                  onPressed: provider.activateEditableState,
                  icon: const Icon(Icons.edit))
            ]
          : null)

Solution

  • It should be fine if you return an empty list instead of null.

    appBar: AppBar(
      title: provider.appBarTitle,
      actions: [
        if (provider.editState == EditState.inactive)
          IconButton(
            onPressed: provider.activateEditableState,
            icon: const Icon(Icons.edit),
          ),
      ],
    ),