今天,我想知道为什么在将这些代码定义到项目中之后,我不能在HookWidget
中使用context.read
:
class FavoriteAddressBottomSheet extends HookWidget {
@override
Widget build(BuildContext context) {
return Container(
child: Material(
color: Colors.transparent,
child: Container(
...
child: Column(
children: [
ValueListenableBuilder(
valueListenable: _address.listenable(),
builder: (BuildContext context, Box address, __) {
return Expanded(
child: ListView.builder(
padding: EdgeInsets.zero,
itemBuilder: (context, index) {
return GestureDetector(
onTap: ()=> context.read(orderStateNotifierProvider.notifier).updateAddress(),
child: Container(
...
);
},
itemCount: _address.length,
),
);
}),
],
),
),
),
),
);
}
}
我的orderStateNotifierProvider
和updateAddress
是正确的,因为我在项目中使用了更多的它们,但是当我试图在ListView
中使用包装成ListView
的ValueListenableBuilder
时,我得到了这个错误:
The method 'read' isn't defined for the type 'BuildContext'.
发布于 2021-08-28 10:04:49
您是否导入了提供程序?context.read是一个来自提供者的扩展方法。要使用它,您必须导入Provider。
import 'package:flutter_riverpod/flutter_riverpod.dart';
https://stackoverflow.com/questions/68966937
复制相似问题