The return type 'String' isn't a 'void', as required by the closure's context

137 views Asked by At

I'm trying to create a function which shows a AlertDialog with a text form field. user will enter a value into this field and then will push the "ok" button. I want to return entered string value when the ok button pressed. but I'm getting The return type 'String' isn't a 'void', as required by the closure's context. error at the return line. Also getting The body might complete normally, causing 'null' to be returned, but the return type, 'String', is a potentially non-nullable type error at the first line.

what should I do?

String enterValue(TextInputType keyType) {
    String girilenDeger = '';
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text('enter_value').tr(),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            TextFormField(
              style: const TextStyle(fontSize: 18, color: Colors.black),
              initialValue: '',
              keyboardType: keyType,
              autofocus: true,
              onChanged: (val) {
                girilenDeger = val;
              },
            ),
          ],
        ),
        actions: [
          ElevatedButton(
            onPressed: () {
              return girilenDeger;
            },
            child: const Text('ok').tr(),
          ),
        ],
      ),
    );
  }
1

There are 1 answers

0
Boby On BEST ANSWER

Please try this

Future<String> enterValue(TextInputType keyType) async {
    String girilenDeger = '';
    await showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text('enter_value'),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            TextFormField(
              style: const TextStyle(fontSize: 18, color: Colors.black),
              initialValue: '',
              keyboardType: keyType,
              autofocus: true,
              onChanged: (val) {
                girilenDeger = val;
              },
            ),
          ],
        ),
        actions: [
          ElevatedButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: const Text('ok')),
        ],
      ),
    );

    return girilenDeger;
  }

btw, i remove your .tr()