“The following ProviderNotFoundException was thrown building ConsumerStreamData.”

huangapple go评论55阅读模式
英文:

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;
  }
}
英文:

“The following ProviderNotFoundException was thrown building ConsumerStreamData.”

main.dart file contain all the providers

  return MultiProvider(
          providers: [
            ChangeNotifierProvider(create: (context)=&gt;AppData()),
            ChangeNotifierProvider(create: (context)=&gt; UtilData()),
            ChangeNotifierProvider(create: (context)=&gt; ButtonData()),
            StreamProvider(create: (context)=&gt; 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(&#39;presed&#39;);
_scaffoldKey1.currentState?.openDrawer();
},
child: Container(
decoration: BoxDecoration(
color: Colors.grey[100], // border color
shape: BoxShape.circle,
),
actions: [
   Consumer&lt;StreamData&gt;(
   builder: (_,value,__){
    if(value == true )
    return Text(&#39;true&#39;);
    else
   return Text(&#39;false&#39;);
     },
    ),
  ],
 ),

this is my stream class that gets bluetooth data

class StreamData{

   Stream&lt;bool&gt; 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&lt;bool&gt;(
   builder: (_,value,__){
    if(value)
    return Text(&#39;true&#39;);
    else
   return Text(&#39;false&#39;);
     },
    ),

huangapple
  • 本文由 发表于 2023年4月17日 02:21:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定