Search code examples
androidiosfluttermobile-applicationnativeapplication

A build function returned null.Build functions must never return null


I am beginner in Flutter programming.I tried an example and it throws error i.e.A build function returned null.Is there something wrong with my code?

Here is my code.

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return  MaterialApp(
      title: 'Startup Name Generator',
      home:  RandomWords(),
    );
  }
}
class RandomWordsState extends State<RandomWords> {
  @override
  Widget build(BuildContext context) {
    final _suggestions = <WordPair>[];
    final _biggerFont = const TextStyle(fontSize: 18.0);
    Widget _buildRow(WordPair pair) {
      return ListTile(
        title: Text(
          pair.asPascalCase,
          style: _biggerFont,
        ),
      );
    }
    Widget _buildSuggestions() {
      return ListView.builder(
          padding: const EdgeInsets.all(16.0),
          itemBuilder: /*1*/ (context, i) {
            if (i.isOdd) return Divider(); /*2*/
            final index = i ~/ 2; /*3*/
            if (index >= _suggestions.length) {
              _suggestions.addAll(generateWordPairs().take(10));
            }
            return _buildRow(_suggestions[index]);
          });
    }
  }
}

class RandomWords extends StatefulWidget {
  @override
  RandomWordsState createState() => RandomWordsState();
}

Below are the errors occurred while running code.

I/flutter ( 1781): A build function returned null.
I/flutter ( 1781): The offending widget is:
I/flutter ( 1781):   RandomWords
I/flutter ( 1781): Build functions must never return null.
I/flutter ( 1781): To return an empty space that causes the building widget to fill available room, return
I/flutter ( 1781): "Container()". To return an empty space that takes as little room as possible, return
I/flutter ( 1781): "Container(width: 0.0, height: 0.0)".
I/flutter ( 1781): 
I/flutter ( 1781): The relevant error-causing widget was:
I/flutter ( 1781):   RandomWords file:///C:/Users/cyberpunk/flutterProjects/flutter_app/lib/main.dart:11:14

Solution

  • You did not return any widget to your build. You need to return a widget in your build which in this case is _buildSuggestions()

    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return  MaterialApp(
          title: 'Startup Name Generator',
          home:  RandomWords(),
        );
      }
    }
    class RandomWordsState extends State<RandomWords> {
      final _suggestions = <WordPair>[];
      final _biggerFont = const TextStyle(fontSize: 18.0);
    
      @override
      Widget build(BuildContext context) {
        return _buildSuggestions(); // return a widget
      }
    
      Widget _buildRow(WordPair pair) {
        return ListTile(
          title: Text(
            pair.asPascalCase,
            style: _biggerFont,
          ),
        );
      }
      Widget _buildSuggestions() {
        return ListView.builder(
            padding: const EdgeInsets.all(16.0),
            itemBuilder: /*1*/ (context, i) {
              if (i.isOdd) return Divider(); /*2*/
              final index = i ~/ 2; /*3*/
              if (index >= _suggestions.length) {
                _suggestions.addAll(generateWordPairs().take(10));
              }
              return _buildRow(_suggestions[index]);
            });
      }
    }
    
    class RandomWords extends StatefulWidget {
      @override
      RandomWordsState createState() => RandomWordsState();
    }