Error in flutter: The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type

Issue

I am new to flutter world

I tried to direct the `TextFormField in this way, and this error in the title appeared

I want to use Directionality textDirection: TextDirection.rtl,

but this what happened

Widget _buildName() {
    Directionality(
      textDirection: TextDirection.rtl,
      child: TextFormField(
      textAlign: TextAlign.right,
      decoration: InputDecoration(labelText: 'الاسم', hintText: 'أدخل اسمك'),
      maxLength: 10,
      validator: (String? value) {
        if (value!.isEmpty) {
          return 'يجب أن لا يكون الحقل فارغًا';
        }
        return null;
      },
      onSaved: (String? value) {
        _name = value;
      },
    ));
  }

Solution

you are getting the error because you are not returning anything. add return keyword before Directionality() and it should work:

Widget _buildName() {
return  Directionality(
      textDirection: TextDirection.rtl,
      child: TextFormField(
      textAlign: TextAlign.right,
      decoration: InputDecoration(labelText: 'الاسم', hintText: 'أدخل اسمك'),
      maxLength: 10,
      validator: (String? value) {
        if (value!.isEmpty) {
          return 'يجب أن لا يكون الحقل فارغًا';
        }
        return null;
      },
      onSaved: (String? value) {
        _name = value;
      },
    ));
  }

Answered By – john

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *