【问题标题】:How do I set the Provider Value from inside the Scaffold in Flutter如何从 Flutter 的 Scaffold 内部设置 Provider 值
【发布时间】:2021-10-31 22:41:46
【问题描述】:

我想从Scaffold 内部设置Provider 的值。但我不确定我该怎么做。

下面列出的是我想在其中设置Provider 值的代码。

return Scaffold(
            body: Column(
              children: [
                SizedBox(height: 2.5.h),
                Padding(
                  padding: EdgeInsets.symmetric(horizontal: 5.w),
                  child: Align(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      " $categoryName Courses",
                      style: TextStyle(
                        fontSize: 10.sp,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                ),
                SizedBox(height: 1.h),
                Expanded(
                  child: CoursePage(
                    courseList: ListCategoryCourses(listScrollDirection: Axis.vertical, categoryName: categoryName),
                  ),
                ),
              ],
            ),
          );

我想在Column 被执行或dispose 方法被执行后设置Provider 值。

但是dispose 方法没有被执行,如何强制dispose 方法被执行?

下面列出的是设置Provider值的代码:

              Provider.of<CategoryProvider>(context, listen: false).currentCategoryDetails(
                currentCategorySelected: false,
                currentCategoryName: null,
              );

【问题讨论】:

    标签: flutter dart flutter-provider


    【解决方案1】:

    尝试在构建方法中创建对象

    var obj = Provider.of<CategoryProvider>(context, listen: false).currentCategoryDetails(
                currentCategorySelected: false,
                currentCategoryName: null,
              );
    

    这个 obj 可以在脚手架或构建方法中的任何地方使用

    【讨论】:

    • 感谢您的回答,但我收到以下编译时错误This expression has a type of 'void' so its value can't be used. Try checking to see if you're using the correct API; there might be a function or call that returns void you didn't expect. Also check type parameters and variables which might also be void.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 2021-06-07
    相关资源
    最近更新 更多