在子小部件方法中访问提供程序变量
Access Provider variable in child widget methods
我正在尝试访问 branchSelected
,一个在我的脚手架子窗口小部件中的代码顶部声明的提供程序。当我在原始小部件中使用 branchSelected.branchDropdownValue 时,它起作用了。但是,当我进入子小部件方法时,特别是
ElevatedButton(
onPressed: () {
branchSelected.branchChange;
/*
refreshBranchData();
refreshProjectData();
*/
},
child: Icon(Icons.refresh))
,我收到错误“未定义名称 'branchSelected'”。查看其他文档,我认为所有的子部件都应该能够访问提供者;但是,这里似乎并非如此。有任何想法吗?我在下面附上了我的代码:
@override
Widget build(BuildContext context) {
final branchSelected = Provider.of<branchListChanges>(context);
return Scaffold(
appBar: AppBar(
title: Text('Build Launcher'),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
_buildTopRow(),
Consumer<branchListChanges>(builder: (context, data, child) {
return Text(
'Current Branch: ${branchSelected.branchDropdownValue}');
}),
_buildList(),
],
),
),
backgroundColor: Colors.grey[200],
);
}
Expanded _buildList() {
// Sets which branch to view for the list runs
return Expanded(
child: RunList(branches, "main"),
);
}
Row _buildTopRow() {
return Row(
children: [
Text("Project:"),
SizedBox(width: 6),
ProjectDropdown(packages),
SizedBox(width: 6),
Text("Branch:"),
SizedBox(width: 6),
BranchDropdown(branchNames),
SizedBox(width: 6),
Checkbox(
checkColor: Colors.white,
fillColor: MaterialStateProperty.all<Color>(Colors.blue),
value: onlyLatestPerDay,
onChanged: (bool? value) {
setState(() {
onlyLatestPerDay = value!;
});
},
),
SizedBox(width: 3),
Text("Only latest per-day"),
SizedBox(width: 6),
Checkbox(
checkColor: Colors.white,
fillColor: MaterialStateProperty.all<Color>(Colors.blue),
value: onlyInstalled,
onChanged: (bool? value) {
setState(() {
onlyInstalled = value!;
});
},
),
SizedBox(width: 3),
Text("Only installed"),
SizedBox(width: 6),
ElevatedButton(
onPressed: () {
branchSelected.branchChange;
/*
refreshBranchData();
refreshProjectData();
*/
},
child: Icon(Icons.refresh))
],
);
}
}
我在下面的 main.dart
中创建了我的提供商:
class HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'cornerCan',
theme: ThemeData(
primarySwatch: Colors.blue,
),
// EFFECTS: runs mainPage function in main_page.dart
home: ChangeNotifierProvider<branchListChanges>(
create: (context) => branchListChanges(),
child: mainPage(),
),
);
}
}
- 您的 branchSelected 变量的范围仅在构建函数内。您需要在构建函数之外声明它才能访问它。
- 您也可以将其作为参数传递给_buildTopRow()函数
例如_buildTopRow(branchSelected)
我正在尝试访问 branchSelected
,一个在我的脚手架子窗口小部件中的代码顶部声明的提供程序。当我在原始小部件中使用 branchSelected.branchDropdownValue 时,它起作用了。但是,当我进入子小部件方法时,特别是
ElevatedButton(
onPressed: () {
branchSelected.branchChange;
/*
refreshBranchData();
refreshProjectData();
*/
},
child: Icon(Icons.refresh))
,我收到错误“未定义名称 'branchSelected'”。查看其他文档,我认为所有的子部件都应该能够访问提供者;但是,这里似乎并非如此。有任何想法吗?我在下面附上了我的代码:
@override
Widget build(BuildContext context) {
final branchSelected = Provider.of<branchListChanges>(context);
return Scaffold(
appBar: AppBar(
title: Text('Build Launcher'),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
_buildTopRow(),
Consumer<branchListChanges>(builder: (context, data, child) {
return Text(
'Current Branch: ${branchSelected.branchDropdownValue}');
}),
_buildList(),
],
),
),
backgroundColor: Colors.grey[200],
);
}
Expanded _buildList() {
// Sets which branch to view for the list runs
return Expanded(
child: RunList(branches, "main"),
);
}
Row _buildTopRow() {
return Row(
children: [
Text("Project:"),
SizedBox(width: 6),
ProjectDropdown(packages),
SizedBox(width: 6),
Text("Branch:"),
SizedBox(width: 6),
BranchDropdown(branchNames),
SizedBox(width: 6),
Checkbox(
checkColor: Colors.white,
fillColor: MaterialStateProperty.all<Color>(Colors.blue),
value: onlyLatestPerDay,
onChanged: (bool? value) {
setState(() {
onlyLatestPerDay = value!;
});
},
),
SizedBox(width: 3),
Text("Only latest per-day"),
SizedBox(width: 6),
Checkbox(
checkColor: Colors.white,
fillColor: MaterialStateProperty.all<Color>(Colors.blue),
value: onlyInstalled,
onChanged: (bool? value) {
setState(() {
onlyInstalled = value!;
});
},
),
SizedBox(width: 3),
Text("Only installed"),
SizedBox(width: 6),
ElevatedButton(
onPressed: () {
branchSelected.branchChange;
/*
refreshBranchData();
refreshProjectData();
*/
},
child: Icon(Icons.refresh))
],
);
}
}
我在下面的 main.dart
中创建了我的提供商:
class HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'cornerCan',
theme: ThemeData(
primarySwatch: Colors.blue,
),
// EFFECTS: runs mainPage function in main_page.dart
home: ChangeNotifierProvider<branchListChanges>(
create: (context) => branchListChanges(),
child: mainPage(),
),
);
}
}
- 您的 branchSelected 变量的范围仅在构建函数内。您需要在构建函数之外声明它才能访问它。
- 您也可以将其作为参数传递给_buildTopRow()函数 例如_buildTopRow(branchSelected)