链接行内的StatefulWidget导致Null值错误



我想在Flutter中制作一个应用程序。我创建了一个Stateless Widget和一个StatefulWidget,并尝试在StatelessWidget中的一行中链接StatefulWidget。但由于某种原因,当我将StatefulWidget放入StatelessWidget中的Row中时,我会得到一个null检查错误。

这是我到目前为止的代码:

```
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
backgroundColor: Colors.black,
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
children: [
IconButton(
icon: Icon(
Icons.add_rounded,
),
color: Colors.white,
iconSize: 45.0,
onPressed: () {},
)
],
),
Column(
children: [
IconButton(
icon: Icon(
Icons.all_inbox_rounded,
),
color: Colors.white,
iconSize: 35.0,
onPressed: () {},
)
],
)
],
),
),
body: Row(
children: [
Courses(),
],
)
),
);
}
}
class Courses extends StatefulWidget {
const Courses({Key key}) : super(key: key);
@override
_CoursesState createState() => _CoursesState();
}
class _CoursesState extends State<Courses> {
@override
Widget build(BuildContext context) {
return Container(
child: ListView(
children: [
Container(
child: Align(
alignment: Alignment.center,
child: Text(
'Content',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
color: Colors.white
),
),
),
),
],
),
);
}
}
```

当运行此代码时,我得到以下错误消息:";对空值使用的空校验运算符";

Courses()封装在Flexible小部件中可以解决此错误。

Row(
children: [
Flexible(child: Courses()),
],
),

最新更新