Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

flutter

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

like image 338
Funny Boss Avatar asked Oct 23 '19 11:10

Funny Boss


People also ask

How do you get rid of null in flutter?

Answer To remove elements where the id is null, you can use the removeWhere and check if the the current Map with the key id is null. myList. removeWhere((e) => e["id"] == null);

How can you avoid NULL check operator used on a NULL value flutter?

Solution 3: Using Fallback Operator: Here, "str" is null, and we set the fallback operator with fallback value in case of "str" is null. You need to do this before using it on the code. You can use this method to handle Null values to escape the "Null check operator used on a null value" error in Flutter or Dart.

How do you assign a null value in flutter?

When creating { question: 'How old are you?' , answer: null } try specify the precise type you want like <String, dynamic>{ question: 'How old are you?' , answer: null } .


1 Answers

@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

like image 187
Constantin N. Avatar answered Oct 17 '22 09:10

Constantin N.