[英]How to solve problem with null values in column?
This is the problem: Column's children must not contain any null values, but a null value was found at index 0这就是问题所在:列的子项不得包含任何 null 值,但在索引 0 处找到了 null 值
I think it has something to do with the map, but i am not sure.我认为这与 map 有关,但我不确定。 I am quite new to coding with dart so any help would be appreciated.我对使用 dart 进行编码非常陌生,因此我们将不胜感激。
And here is the code:这是代码:
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
home: Main(),
));
int counter = 0;
class Main extends StatefulWidget {
@override
_MainState createState() => _MainState();
}
class _MainState extends State<Main> {
static List<String> names = [
'name1',
'name2',
];
static List<String> difficulty = [
'easy',
'normal',
];
String currentDifficulty = difficulty[counter];
var count = names.length;
@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: names.map((name) {
Container(
child: Column(
children: <Widget>[
Text(
name
),
Text(
'currentDifficulty'
),
],
),
);
counter += 1;
}).toList(),
),
);
}
}
If you want to know the index of each widget you are creating on a map function from a list I would suggest to use List.asMap().map((index, string)=> MapEntry(index, widget())).values.toList()
since the map function alone does not allow to get the index如果您想从列表中了解您在 map function 上创建的每个小部件的索引,我建议使用List.asMap().map((index, string)=> MapEntry(index, widget())).values.toList()
因为 map function 单独不允许获取索引
Try substituting your children code for:尝试将您的孩子代码替换为:
names.asMap().map((index, name)=> MapEntry(index, Container(
child: Column(
children: <Widget>[
Text(
name
),
Text(
difficulty[index]
),
],
),
))).values.toList();
You are getting the errors because:您收到错误是因为:
1) You are accessing the elements of your List
wrongly. 1)您错误地访问了List
的元素。 To access elements in a List, use the elementAt
method要访问 List 中的元素,请使用elementAt
方法
2) The children property of your Column
is missing a return statement. 2)您的Column
的 children 属性缺少返回语句。
3) Instead of using a counter to iterate through the second list. 3)而不是使用计数器来遍历第二个列表。 You can map through the two lists using the IterableZip
.您可以使用IterableZip
通过两个列表 map 。
Check the code below: It solves the errors and it works fine检查下面的代码:它解决了错误并且工作正常
int counter = 0;
class MyHomePage extends StatelessWidget {
static List<String> names = [
'name1',
'name2',
];
static List<String> difficulty = [
'easy',
'normal',
];
// access elements in a list using the elementAt function
String currentDifficulty = difficulty.elementAt(counter);
@override
Widget build(BuildContext context) {
names.map((e) => print(e));
return Scaffold(
body: Center(
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
// map the two lists using IterableZip and passing the two lists
children: IterableZip([names, difficulty]).map(
(element) {
// missing return statement
return Container(
child: Column(
children: <Widget>[
// access elements of your first list here
Text(element[0]),
// access elements of your second list here
Text(element[1]),
],
),
);
},
).toList(),
),
),
),
);
}
}
OUTPUT OUTPUT
I hope this helps.我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.