flutter - How can I Initialize provider?
问题描述
I have implemented Provider for state management in my app. Now, I need to add some data in the class once the screen is loaded.
How I can achieve this?
stepInfo.addToList = new VaccStep(); // Need to call it one time once screen is loaded.
I have tried to call this method from initState but it's giving error!!
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();
}
[...]
}
推荐阅读
- asp.net-mvc - 如何在MVC中使用if条件更改html标签数据
- node.js - 如何在 nodejs 中获取动态 url?(人栈)
- javascript - 如何将一个数字尽可能均匀地分配到一个数组中?
- frameworks - 框架不可知论是什么意思?
- python - sqlite3.OperationalError:没有这样的表:时间表
- python - 如何确定函数是否返回按需计算结果的可迭代对象?
- amazon-web-services - 如何使 HTTPS url/链接适用于我的亚马逊 EC2 实例?
- angular - 无法在角度应用程序中查看库中的图像
- javascript - 多行刷卡器无法从 swiper.js 做出反应
- reactjs - 为 django admin 和 react 应用程序一起服务时的问题