英文:
Streamprovider i am getting this issue "The following ProviderNotFoundException was thrown building ConsumerStreamData
问题
main.dart文件包含所有的提供者。
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => AppData()),
ChangeNotifierProvider(create: (context) => UtilData()),
ChangeNotifierProvider(create: (context) => ButtonData()),
StreamProvider(create: (context) => streamProvider.getScanningData(), initialData: false),
],
);
在另一个StatefulWidget类中的AppBar中,我正在尝试使用Consumer widget获取该流的值,我得到了这个:
appBar: AppBar(
leading: Padding(
padding: EdgeInsets.only(
left: SizeConfig.blockSizeHorizontal * 1.5,
),
child: InkWell(
onTap: () {
print('pressed');
_scaffoldKey1.currentState?.openDrawer();
},
child: Container(
decoration: BoxDecoration(
color: Colors.grey[100], // 边框颜色
shape: BoxShape.circle,
),
actions: [
Consumer<StreamData>(
builder: (_, value, __) {
if (value == true)
return Text('true');
else
return Text('false');
},
),
],
),
),
),
);
这是我的Stream类,用于获取蓝牙数据:
class StreamData {
Stream<bool> getScanningData() {
return FlutterBluePlus.instance.isScanning;
}
}
英文:
main.dart file contain all the providers
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (context)=>AppData()),
ChangeNotifierProvider(create: (context)=> UtilData()),
ChangeNotifierProvider(create: (context)=> ButtonData()),
StreamProvider(create: (context)=> streamProvider.getScanningData(),
initialData: false),
],
so in another statefulwidget class in appbar im trying to get that stream value using consumer widget and i am getting this ,
appBar: AppBar(
leading: Padding(
padding: EdgeInsets.only(
left: SizeConfig.blockSizeHorizontal * 1.5,
),
child: InkWell(
onTap: (){
print('presed');
_scaffoldKey1.currentState?.openDrawer();
},
child: Container(
decoration: BoxDecoration(
color: Colors.grey[100], // border color
shape: BoxShape.circle,
),
actions: [
Consumer<StreamData>(
builder: (_,value,__){
if(value == true )
return Text('true');
else
return Text('false');
},
),
],
),
this is my stream class that gets bluetooth data
class StreamData{
Stream<bool> getScanningData(){
return FlutterBluePlus.instance.isScanning;
}
答案1
得分: 2
以下是翻译好的部分:
在消费者小部件中,您的代码应为
Consumer<bool>(
builder: (_, value, __) {
if (value)
return Text('true');
else
return Text('false');
},
),
英文:
In consumer widget you code should be
Consumer<bool>(
builder: (_,value,__){
if(value)
return Text('true');
else
return Text('false');
},
),
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论