Home > OS >  Flutter Expected a value of type 'Widget', but got one of type 'Null'
Flutter Expected a value of type 'Widget', but got one of type 'Null'

Time:01-13

I'm trying to create a flutter app which uses Textinput. I created TextInput as class named "Textinput" and function "renderTextInput" to have more compotibility I set values in TextInput by using class TextInputData with gets and sets. Code doesn't mark me errors.

class Textinput {
  void renderTextInput(TextInputData textInputData) {
    Container(
      child: TextFormField(
        decoration: InputDecoration(labelText: textInputData.getText()),
        keyboardType: textInputData.getType(),
        style: textInputData.getStyle(),
      ),
      width: 400.0,
    );
  }
}
class TextInputData {
  String? _text;
  TextInputType? _type;
  TextStyle? _style;
  TextInputData(text, type, style) {
    _text = text;
    _style = style;
    _type = type;
  }
  String? getText() {
    return _text;
  }

  void setText(text) {
    _text = text;
  }

  TextInputType? getType() {
    return _type;
  }

  void setType(type) {
    _type = type;
  }

  TextStyle? getStyle() {
    return _style;
  }

  void setStyle(style) {
    _style = style;
  }
}

usage in code

textInput.renderTextInput(TextInputData(
                "почта", TextInputType.emailAddress, _sizeTextBlack))

and how I call it in main

void main() {
  runApp(MaterialApp(title: 'Navigation Basics', home: AuthorizationScreen()));
}

If you know how to solve it please tell me. I would really appreciate.

CodePudding user response:

Based on your code structure, I think you need to return the widget

class Textinput {
  Widget renderTextInput(TextInputData textInputData) {
    return Container(
      child: TextFormField(
        decoration: InputDecoration(labelText: textInputData.getText()),
        keyboardType: textInputData.getType(),
        style: textInputData.getStyle(),
      ),
      width: 400.0,
    );
  }
}

CodePudding user response:

First of all, remember that void means Function doesn’t return anything. Bit you need to return something here.

Secondly, It is better to make it stateless widget instead of simple class. Just create a stateless Widget class with a parameter you are giving to the function. And return the Widget in build method. It is much more standard and proper way.

  • Related