如何初始化提供程序?

时间:2019-06-10 14:30:07

标签: flutter flutter-provider

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

我该如何实现?

stepInfo.addToList = new VaccStep(); //屏幕载入后需要调用一次。

我试图从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<StepInfo> above this AdminAddVaccination Widget
    flutter:
    flutter: To fix, please:
    flutter:
    flutter:   * Ensure the Provider<StepInfo> is an ancestor to this AdminAddVaccination Widget
    flutter:   * Provide types to Provider<StepInfo>
    flutter:   * Provide types to Consumer<StepInfo>
    flutter:   * Provide types to Provider.of<StepInfo>()
    flutter:   * Always use package imports. Ex: `import 'package:my_app/my_code.dart';

3 个答案:

答案 0 :(得分:1)

您必须将listen:false设置为initstate并设置一些延迟

Provider.of<StepInfo>(context, listen: false);

Future.delayed(Duration(milliseconds: 100)).then((_) {
stepInfo.addToList = new VaccStep();
});

thisthis情况下相同

答案 1 :(得分:0)

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

class StepInfo extends ChangeNotifier {

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

   [...]

}

答案 2 :(得分:0)

更改 AdminAddVaccination 类中的 initState() build()方法,如下所示:

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var stepInfo = new StepInfo();
    stepInfo.addToList = new VaccStep();
    return new ChangeNotifierProvider<StepInfo>(
      builder: (context) => stepInfo,
      child: ScreenBody(),
    );
  }