[英]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.