我是一个新的flutter开发者,而工作的flutter UI模板,我发现在网上,我遇到了这个错误,我似乎无法修复。
以下是错误:在构建Builder时抛出了以下_TypeError:用于空值的空检查运算符
导致错误的相关小部件为:MaterialApp MaterialApp:file:/C:/Users/Owner/Desktop/Dzidzo/Mobile%20application/dzidzo/lib/main.dart:21:18
当抛出异常时,这是堆栈:#0 _MyDiaryScreenState.initState(包:dzidzo/fitness_app/my_diary/my_diary_screenState.dart:30:47)#1 StatefulElement._firstBuild(包:flutter/src/widgets/framework.dart:5219:55)#2 ComponentElement.mount(包:flutter/src/widgets/framework.dart:5062:5)
下面是可能导致错误的代码:
class MyDiaryScreen extends StatefulWidget {
const MyDiaryScreen({Key? key, this.animationController}) : super(key: key);
final AnimationController? animationController;
@override
_MyDiaryScreenState createState() => _MyDiaryScreenState();
}
class _MyDiaryScreenState extends State<MyDiaryScreen>
with TickerProviderStateMixin {
Animation<double>? topBarAnimation;
List<Widget> listViews = <Widget>[];
final ScrollController scrollController = ScrollController();
double topBarOpacity = 0.0;
@override
void initState() {
topBarAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(
CurvedAnimation(
parent: widget.animationController!,
curve: Interval(0, 0.5, curve: Curves.fastOutSlowIn)));
addAllListData();
scrollController.addListener(() {
if (scrollController.offset >= 24) {
if (topBarOpacity != 1.0) {
setState(() {
topBarOpacity = 1.0;
});
}
} else if (scrollController.offset <= 24 &&
scrollController.offset >= 0) {
if (topBarOpacity != scrollController.offset / 24) {
setState(() {
topBarOpacity = scrollController.offset / 24;
});
}
} else if (scrollController.offset <= 0) {
if (topBarOpacity != 0.0) {
setState(() {
topBarOpacity = 0.0;
});
}
}
});
super.initState();
}
void addAllListData() {
const int count = 9;
listViews.add(
TitleView(
titleTxt: 'Mediterranean diet',
subTxt: 'Details',
animation: Tween<double>(begin: 0.0, end: 1.0).animate(CurvedAnimation(
parent: widget.animationController!,
curve:
Interval((1 / count) * 0, 1.0, curve: Curves.fastOutSlowIn))),
animationController: widget.animationController!,
),
);
listViews.add(
MediterranesnDietView(
animation: Tween<double>(begin: 0.0, end: 1.0).animate(CurvedAnimation(
parent: widget.animationController!,
curve:
Interval((1 / count) * 1, 1.0, curve: Curves.fastOutSlowIn))),
animationController: widget.animationController!,
),
);
listViews.add(
TitleView(
titleTxt: 'Meals today',
subTxt: 'Customize',
animation: Tween<double>(begin: 0.0, end: 1.0).animate(CurvedAnimation(
parent: widget.animationController!,
curve:
Interval((1 / count) * 2, 1.0, curve: Curves.fastOutSlowIn))),
animationController: widget.animationController!,
),
);
listViews.add(
MealsListView(
mainScreenAnimation: Tween<double>(begin: 0.0, end: 1.0).animate(
CurvedAnimation(
parent: widget.animationController!,
curve: Interval((1 / count) * 3, 1.0,
curve: Curves.fastOutSlowIn))),
mainScreenAnimationController: widget.animationController,
),
);
listViews.add(
TitleView(
titleTxt: 'Body measurement',
subTxt: 'Today',
animation: Tween<double>(begin: 0.0, end: 1.0).animate(CurvedAnimation(
parent: widget.animationController!,
curve:
Interval((1 / count) * 4, 1.0, curve: Curves.fastOutSlowIn))),
animationController: widget.animationController!,
),
);
listViews.add(
BodyMeasurementView(
animation: Tween<double>(begin: 0.0, end: 1.0).animate(CurvedAnimation(
parent: widget.animationController!,
curve:
Interval((1 / count) * 5, 1.0, curve: Curves.fastOutSlowIn))),
animationController: widget.animationController!,
),
);
listViews.add(
TitleView(
titleTxt: 'Water',
subTxt: 'Aqua SmartBottle',
animation: Tween<double>(begin: 0.0, end: 1.0).animate(CurvedAnimation(
parent: widget.animationController!,
curve:
Interval((1 / count) * 6, 1.0, curve: Curves.fastOutSlowIn))),
animationController: widget.animationController!,
),
);
listViews.add(
WaterView(
mainScreenAnimation: Tween<double>(begin: 0.0, end: 1.0).animate(
CurvedAnimation(
parent: widget.animationController!,
curve: Interval((1 / count) * 7, 1.0,
curve: Curves.fastOutSlowIn))),
mainScreenAnimationController: widget.animationController!,
),
);
listViews.add(
GlassView(
animation: Tween<double>(begin: 0.0, end: 1.0).animate(
CurvedAnimation(
parent: widget.animationController!,
curve: Interval((1 / count) * 8, 1.0,
curve: Curves.fastOutSlowIn))),
animationController: widget.animationController!),
);
}
Future<bool> getData() async {
await Future<dynamic>.delayed(const Duration(milliseconds: 50));
return true;
}
@override
Widget build(BuildContext context) {
return Container(
color: FitnessAppTheme.background,
child: Scaffold(
backgroundColor: Colors.transparent,
body: Stack(
children: <Widget>[
getMainListViewUI(),
getAppBarUI(),
SizedBox(
height: MediaQuery.of(context).padding.bottom,
)
],
),
),
);
}
Widget getMainListViewUI() {
return FutureBuilder<bool>(
future: getData(),
builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
if (!snapshot.hasData) {
return const SizedBox();
} else {
return ListView.builder(
controller: scrollController,
padding: EdgeInsets.only(
top: AppBar().preferredSize.height +
MediaQuery.of(context).padding.top +
24,
bottom: 62 + MediaQuery.of(context).padding.bottom,
),
itemCount: listViews.length,
scrollDirection: Axis.vertical,
itemBuilder: (BuildContext context, int index) {
widget.animationController?.forward();
return listViews[index];
},
);
}
},
);
}
Widget getAppBarUI() {
return Column(
children: <Widget>[
AnimatedBuilder(
animation: widget.animationController!,
builder: (BuildContext context, Widget? child) {
return FadeTransition(
opacity: topBarAnimation!,
child: Transform(
transform: Matrix4.translationValues(
0.0, 30 * (1.0 - topBarAnimation!.value), 0.0),
child: Container(
decoration: BoxDecoration(
color: FitnessAppTheme.white.withOpacity(topBarOpacity),
borderRadius: const BorderRadius.only(
bottomLeft: Radius.circular(32.0),
),
boxShadow: <BoxShadow>[
BoxShadow(
color: FitnessAppTheme.grey
.withOpacity(0.4 * topBarOpacity),
offset: const Offset(1.1, 1.1),
blurRadius: 10.0),
],
),
child: Column(
children: <Widget>[
SizedBox(
height: MediaQuery.of(context).padding.top,
),
Padding(
padding: EdgeInsets.only(
left: 16,
right: 16,
top: 16 - 8.0 * topBarOpacity,
bottom: 12 - 8.0 * topBarOpacity),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'My Diary',
textAlign: TextAlign.left,
style: TextStyle(
fontFamily: FitnessAppTheme.fontName,
fontWeight: FontWeight.w700,
fontSize: 22 + 6 - 6 * topBarOpacity,
letterSpacing: 1.2,
color: FitnessAppTheme.darkerText,
),
),
),
),
SizedBox(
height: 38,
width: 38,
child: InkWell(
highlightColor: Colors.transparent,
borderRadius: const BorderRadius.all(
Radius.circular(32.0)),
onTap: () {},
child: Center(
child: Icon(
Icons.keyboard_arrow_left,
color: FitnessAppTheme.grey,
),
),
),
),
Padding(
padding: const EdgeInsets.only(
left: 8,
right: 8,
),
child: Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 8),
child: Icon(
Icons.calendar_today,
color: FitnessAppTheme.grey,
size: 18,
),
),
Text(
'15 May',
textAlign: TextAlign.left,
style: TextStyle(
fontFamily: FitnessAppTheme.fontName,
fontWeight: FontWeight.normal,
fontSize: 18,
letterSpacing: -0.2,
color: FitnessAppTheme.darkerText,
),
),
],
),
),
SizedBox(
height: 38,
width: 38,
child: InkWell(
highlightColor: Colors.transparent,
borderRadius: const BorderRadius.all(
Radius.circular(32.0)),
onTap: () {},
child: Center(
child: Icon(
Icons.keyboard_arrow_right,
color: FitnessAppTheme.grey,
),
),
),
),
],
),
)
],
),
),
),
);
},
)
],
);
}
}
字符串
我在网上尝试了很多解决方案,说了一些关于替换空操作符的东西!”但我似乎不能让它工作
1条答案
按热度按时间kmynzznz1#
您遇到的错误是由于使用了null检查运算符(!)的可空值
widget.animationController
。您的
MyDiaryScreen
小部件依赖于AnimationController
,该小部件当前为空。要解决这个问题,请将animationController
参数设为不可空,并且在将其作为参数传递时是必需的。这样,您就不需要使用空校验运算符(!)在整个widget代码中。字符串
每次使用MyDiaryScreen小部件时,请确保提供必要的animationController。
或者
考虑是否有必要提供动画控制器作为参数。如果可以在
MyDiaryScreen
中创建新的动画控制器,则可以进行以下更改:型
这样,您就可以在
MyDiaryScreen
小部件中创建一个新的AnimationController
,从而避免了外部初始化的需要。