简体   繁体   English

如何解决 Flutter 错误列的子项不得包含任何 null 值,但在索引 0 处找到 null 值?

[英]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 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 stateul 小部件代码是: 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:事务 class 如下所示:

Transaction.dart 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 theftion trible tribrars捕获的例外╞═════════════════════════════════════════ ══════════════════ 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在索引 0 相关的导致错误的小部件是: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.当我要调试时,我正在使用打印线找到正确的 output。 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您收到此错误是因为您没有返回任何值,因此您的tolist()方法正在重新调整 null 对象列表

I think, perhaps you have not using widgets like Column children and flutter cannot paint them.我想,也许你没有使用像 Column children 这样的小部件,flutter 无法绘制它们。 "Transaction" is not a widget. “交易”不是小部件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 错误列的子项不得包含任何 null 值,但在索引 0 处找到 null 值 - Flutter error Column's children must not contain any null values, but a null value was found at index 0 Flutter 错误:列的子项不得包含任何 null 值,但在索引 0 处找到 null 值 - Flutter error: Column's children must not contain any null values, but a null value was found at index 0 Flutter 函数错误:列的子项不得包含任何空值,但在索引 1 处发现空值 - Flutter function error: Column's children must not contain any null values, but a null value was found at index 1 列的子项不得包含任何 null 值,但在索引 13 处找到 null 值。 Flutter Z866408593C2F8BDF27 - Column's children must not contain any null values, but a null value was found at index 13. Flutter Function 列的子项不得包含任何 null 值,但在索引 0 处找到 null 值 - Column's children must not contain any null values, but null value was found at index 0 OOP 错误行的子项不得包含任何 null 值,但在索引 0 处找到了 null 值 - OOP Error Row's children must not contain any null values, but a null value was found at index 0 行的子项不得包含任何 null 值,但在索引 2 处找到 null 值 - Row's children must not contain any null values, but a null value was found at index 2 行的孩子不得包含任何 Null 值 Flutter 错误 - Row's Children Must Not Contain Any Null Values Flutter Error 如何解决在flutter中在null上调用getter&#39;value&#39;的问题 - How to solve the getter 'value' was called on null in flutter 如何解决列中 null 值的问题? - How to solve problem with null values in column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM