简体   繁体   English

Flutter - 如何处理小部件属性的非空实例

[英]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.有两种方法可以解决这个问题。

  1. 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关键字回答了这个问题,您必须在任何地方使用之前对其进行初始化。

  2. 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.

相关问题 不可为空的实例字段 '_bmi' 必须初始化 flutter - Non-nullable instance field '_bmi' must be initialized flutter 不可为空的实例字段 - Non-Nullable instance field 可空值和不可空值 - Nullable and non-nullable values 如何创建可以保存 state 的不可为空的 LiveData - How to create a non-nullable LiveData that can save state 错误:主体可能正常完成,导致返回“null”,但返回类型“Widget”可能是不可为 null 的类型 - error: The body might complete normally, causing 'null' to be returned, but the return type, 'Widget', is a potentially non-nullable type MutableLiveData 的预期不可为空值 - Expected non-nullable value for MutableLiveData 你如何让 Mockito 与 Kotlin 不可空类型一起玩得很好? - How do you get Mockito to play nice with Kotlin non-nullable types? 无法将不可为空的 LiveData 值设置为 null - Cannot set non-nullable LiveData value to null Kotlin 单元测试变量声明 lateinit vs lazy vs nullable vs non nullable - Kotlin Unit Test variable declaration lateinit vs lazy vs nullable vs non-nullable 为什么可以将 java 可空返回值分配给 Kotlin 非可空变量? - Why can a java nullable returned value be assigned to a Kotlin Non-Nullable variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM