[英]Flutter - How to handle Non-Nullable Instance of widget properties
I am new to flutter and currently trying to simple animation on my component, but i keep getting a " Non-Nullable Instance " error, i checked out other video tutorials but theirs worked perfectly while mine keeps throwing errors.我是 flutter 的新手,目前正尝试在我的组件上简单 animation,但我不断收到“不可为空的实例”错误,我查看了其他视频教程,但他们的工作完美,而我的不断抛出错误。
Here's my code.这是我的代码。
class HomeScreen extends StatefulWidget {
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {
Animation<Offset> sbarAnimation;
AnimationController sbarAnimationController;
@override
void initState() {
super.initState();
sbarAnimationController = AnimationController(
vsync: this,
duration: Duration(milliseconds: 250),
);
sbarAnimation = Tween<Offset>(
begin: Offset(-1, 0),
end: Offset(0, 0),
).animate(
CurvedAnimation(
parent: sbarAnimationController,
curve: Curves.easeInOut,
),
);
}
And then the code for the widget is below然后小部件的代码如下
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
color: kBackgroundColor,
child: Stack(
children: [
SafeArea(
//so the background covers the entire app..
child: Column(
children: [
HomeScreenNavBar(),
ExploreCourseList(),
],
),
),
SlideTransition(
position: sbarAnimation, // this is where i call the _animation
child: SafeArea(
child: SidebarScreen(),
bottom: false,
),
),
],
),
),
);
}
}
below is the errors i keep getting, and most of the solutions i've seen did exactly the same thing, theirs works, but mine is giving this errors.. please what am i supposed to do.以下是我不断遇到的错误,我见过的大多数解决方案都做了同样的事情,他们的作品,但我的给出了这个错误..请问我应该怎么做。
There two way to solve the issue.有两种方法可以解决这个问题。
Which already answered by Juan Carlos Ramón Condezo , using late keyword, where you must have to initialize it before using anywhere. Juan Carlos Ramón Condezo已经使用late关键字回答了这个问题,您必须在任何地方使用之前对其进行初始化。
You can make the variable nullable.您可以使变量为空。 By using '?'
通过使用 '?' After declaring the variable type.
声明变量类型后。
Animation?<Offset> sbarAnimation;
AnimationController? sbarAnimationController;
So, you can check if the variable is null and initialize it when needed.因此,您可以检查变量是否为 null 并在需要时对其进行初始化。
Use late
keyword to initialize variables later as you are doing in your initState
稍后在
initState
中使用late
关键字来初始化变量
late Animation<Offset> sbarAnimation;
late AnimationController sbarAnimationController;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.