Funny Boss
Funny Boss

Reputation: 338

How to solve Flutter error Column's children must not contain any null values, but a null value was found at index 0?

I was learning and trying to create one app using one stateful widget to display a list. My code looks like the following:

main.dart

import 'package:flutter/material.dart';
import './widgets/user_transactions.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Container(
          child: UserTransactions(),
        ),
      ),
    );
  }
}

and the stateul widget code is: user_transactions.dart

import 'package:flutter/material.dart';
import '../models/transaction.dart';

class UserTransactions extends StatefulWidget {
  @override
  _UserTransactionsState createState() => _UserTransactionsState();
}

class _UserTransactionsState extends State<UserTransactions> {
  final List<Transaction> _userTransactionLists = [
    Transaction(name: 'boss 01'),
    Transaction(name: 'boss 02'),
    Transaction(name: 'boss 03'),
    Transaction(name: 'boss 04'),
  ];
  @override
  Widget build(BuildContext context) {
    print('==========================================');
    print(_userTransactionLists.length);
    return Column(
      children: _userTransactionLists.map((tx) {
        Text(tx.name);
        print(tx.name);
      }).toList(),
    );
  }
}

And the transaction class looks like this:

Transaction.dart

import 'package:flutter/foundation.dart';

class Transaction {
  final String name;
  Transaction({@required this.name});
}

But getting the error:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ The following assertion was thrown building UserTransactions(dirty, state: _UserTransactionsState#c1fe3): Column's children must not contain any null values, but a null value was found at index 0 The relevant error-causing widget was: UserTransactions org-dartlang-app:///packages/test_01/main.dart:14:18

I tried long time but still could not figure out. And when I was tying to debug, I am geeing the correct output using print line. It looks like the following:

enter image description here

Upvotes: 11

Views: 20853

Answers (1)

Constantin N.
Constantin N.

Reputation: 2839

@override
Widget build(BuildContext context) {
    print('==========================================');
    print(_userTransactionLists.length);
    return Column(
      children: _userTransactionLists.map((tx) {
       print(tx.name);
       return Text(tx.name);
      }).toList(),
    );
  }

You are getting this error because you didn't return any value so, your tolist() method was retuning a list of null objects

Upvotes: 24

Related Questions