【发布时间】:2023-04-09 16:47:02
【问题描述】:
有我的代码: StreamBuilder 有错误
错误参数类型“对象?”不能分配给参数 输入'颜色?
body: Center(
child: StreamBuilder(
stream: bloc.stateStream,
initialData: Colors.amber,
builder: (context, snapshot) {
return AnimatedContainer(
duration: Duration(milliseconds: 500),
width: 100,
height: 100,
color: snapshot.data,
);
},
),
),
body: Center(
child: StreamBuilder(
stream: bloc.stateStream,
initialData: Colors.amber,
builder: (context, snapshot) {
return AnimatedContainer(
duration: Duration(milliseconds: 500),
width: 100,
height: 100,
color: snapshot.data,
);
},
),
),
),
);
},
}
【问题讨论】:
-
您正在将类型 Object 分配给颜色属性,颜色属性需要颜色类型的值。你的 stateStream 是否返回颜色?
-
是的,我要返回颜色
-
你能用 BLoC 更新你的代码吗?
-
您可以格式化代码以获得更好的可读性。
-
不要直接传递:color:snapshot.data,而是在return语句前使用color的变量,然后在color属性中使用。
标签: flutter