flutter - 如何初始化提供商?

标签 flutter flutter-provider

我已经在我的应用程序中实现了用于状态管理的提供程序。现在,一旦屏幕加载,我需要在类中添加一些数据。

我怎样才能实现这个目标?

stepInfo.addToList = new VaccStep(); // Need to call it one time once screen is loaded.

我尝试从 initState 调用此方法,但出现错误!!

class AdminAddVaccination extends StatefulWidget {
  @override
  State createState() => new AdminAddVaccinationState();
}

class AdminAddVaccinationState extends State<AdminAddVaccination> {

  @override
  void initState() {
    super.initState();
    var stepInfo = Provider.of<StepInfo>(context); // ERROR!!
    stepInfo.addToList = new VaccStep(); // ERROR!!
  }

  Widget build(BuildContext context) {
    return new ChangeNotifierProvider(
      builder: (context) => StepInfo(),
      child: ScreenBody(),
    );
  }
}

class ScreenBody extends StatelessWidget {


  @override
  Widget build(BuildContext context) {
    var stepInfo = Provider.of<StepInfo>(context);

    return new Scaffold(
        resizeToAvoidBottomPadding: false,
        key: stepInfo.scaffoldKey,
        body: new GestureDetector(
            onTap: () {
              FocusScope.of(context).requestFocus(new FocusNode());
            },
            child: new SafeArea(
              top: true,
              bottom: false,
              child: new Stack(children: <Widget>[
                new Opacity(
                  opacity: 0.04,
                  child: new Image.asset(
                    "assets/userProfile/heartBeat.png",
                    fit: BoxFit.cover,
                    height: 250.0,
                  ),
                ),
                new Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    new Container(
                      color: primaryGreen,
                      width: double.infinity,
                      height: 65.0,
                      child: new Stack(
                        children: <Widget>[
                          new Align(
                              alignment: Alignment.center,
                              child: stepInfo.loading
                                  ? JumpingText('......')
                                  : new Container()),
                          new Align(
                            alignment: Alignment.centerLeft,
                            child: new Padding(
                              padding: EdgeInsets.only(top: 5.0, left: 20.0),
                              child: new InkWell(
                                child: new Container(
                                  child: Icon(
                                    Icons.arrow_back,
                                    color: Colors.white,
                                    size: 30.0,
                                  ),
                                ),
                                onTap: () {
                                  Navigator.pop(context);
                                },
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    new Padding(
                      padding: EdgeInsets.only(top: 0.0),
                      child: new Material(
                        elevation: 1.0,
                        color: Colors.transparent,
                        child: new Container(
                          color: borderColor,
                          width: double.infinity,
                          height: 5.0,
                        ),
                      ),
                    ),
                    VaccName(),
                  ],
                ),
                ItemListing(),
                AddStep(),
              ]),
            )));
  }
}

Error!! flutter: The following ProviderNotFoundError was thrown building Builder: flutter: Error: Could not find the correct Provider above this AdminAddVaccination Widget flutter: flutter: To fix, please: flutter: flutter: * Ensure the Provider is an ancestor to this AdminAddVaccination Widget flutter: * Provide types to Provider flutter: * Provide types to Consumer flutter: * Provide types to Provider.of() flutter: * Always use package imports. Ex: `import 'package:my_app/my_code.dart';

最佳答案

只需在您的提供程序中添加一个构造函数:

class StepInfo extends ChangeNotifier {

   StepInfo() {
      this.addToList = new VaccStep();
   }

   [...]

}

关于flutter - 如何初始化提供商?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56528381/

相关文章:

flutter - 如何在Flutter/Dart中处理键盘 Action 或在键盘TextInputAction中添加功能?

android - Http 请求在 Flutter 中不返回任何内容,也不会抛出任何错误

flutter - Flutter Provider.of <MyProvider>(…)返回null

flutter - 在包含导航流的Flutter中,此Y小部件上方找不到正确的Provider <X>

flutter - 如何在 Flutter 中显示 SnackBar

flutter - 如何为 macos 桌面应用启用 Flutter 上网权限?

post - Flutter - 处理POST请求中的状态码302

flutter - 找不到正确的提供者

flutter - 允许提供者之间的区别

flutter - 将提供程序包添加到 Flutter Web 项目时出错