flutter 如何初始化提供程序?

bfrts1fy  于 2023-06-07  发布在  Flutter
关注(0)|答案(4)|浏览(201)

我已经在我的应用程序中实现了状态管理提供程序。现在,我需要在屏幕加载后在类中添加一些数据。
我如何才能做到这一点?

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:生成生成器时引发以下ProviderNotFoundError:Flutter:错误:无法在此AdminAddVaccination Widget flutter上方找到正确的提供者:flutter:要修复,请:扑动:扑动:* 确保提供者是此AdminAddVaccination Widget flutter的祖先:* 提供类型到提供商flutter:* 为消费者提供类型:* 提供类型给Provider.of()flutter:* 始终使用包导入。例如:`import '包:my_app/my_code. dart';

mrzz3bfm

mrzz3bfm1#

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

class StepInfo extends ChangeNotifier {

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

   [...]

}
o7jaxewo

o7jaxewo2#

必须在initstate上设置listen:false和一些延迟

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

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

thisthis情况下相同

tcbh2hod

tcbh2hod3#

修改AdminAddVaccination类中的initState()&**build()**方法如下:

var stepInfo;
  @override
  void initState() {
    super.initState();
    stepInfo = new StepInfo();
    stepInfo.addToList = new VaccStep();
  }

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

insrf1ej4#

addPostFrameCallback()

这个问题的解决方案是实现addPostFrameCallback,在帧的末尾调度回调。它在上下文可用时执行该方法。

@override
void initState() {
  super.initState();
  WidgetsBinding.instance.addPostFrameCallback((_) {
      var stepInfo = Provider.of<StepInfo>(context,listen:false);
      stepInfo.addToList = new VaccStep(); 
  });
}

相关问题