简体   繁体   中英

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:

在此处输入图像描述

@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

I think, perhaps you have not using widgets like Column children and flutter cannot paint them. "Transaction" is not a widget.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM