Flutter 找不到正确的提供程序-Flatter ChangeNotificationProvider
错误:在此ProductScreen小部件上方找不到正确的提供程序 我尝试在我的Flitter应用程序中使用MVVM时遇到此错误。ProductScreen上面有一个提供者,它是ProductListViewModel,我认为它应该可以工作。如果你能给我反馈,我将不胜感激 ChangeNotifierProvider用法:Flutter 找不到正确的提供程序-Flatter ChangeNotificationProvider,flutter,flutter-layout,Flutter,Flutter Layout,错误:在此ProductScreen小部件上方找不到正确的提供程序 我尝试在我的Flitter应用程序中使用MVVM时遇到此错误。ProductScreen上面有一个提供者,它是ProductListViewModel,我认为它应该可以工作。如果你能给我反馈,我将不胜感激 ChangeNotifierProvider用法: class MyApp extends StatelessWidget { @override Widget build(BuildContext context
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Products",
debugShowCheckedModeBanner: false,
home:
ChangeNotifierProvider(
create: (context) => ProductListViewModel(),
child: ProductScreen(),
)
);
}
}
ProductListViewModel
class ProductListViewModel extends ChangeNotifier {
List<ProductViewModel> products = List<ProductViewModel>();
Future<void> fetchProducts(String keyword) async {
final results = await Webservice().fetchProducts(keyword);
this.products = results.map((item) => ProductViewModel.fromProduct(item)).toList();
notifyListeners();
}
}
类ProductListViewModel扩展了ChangeNotifier{
列表产品=列表();
未来fetchProducts(字符串关键字)异步{
最终结果=等待Webservice().fetchProducts(关键字);
this.products=results.map((项)=>ProductViewModel.fromProduct(项)).toList();
notifyListeners();
}
}
产品屏幕
class ProductScreen extends StatefulWidget {
@override
_ProductScreenState createState() => _ProductScreenState();
}
class _ProductScreenState extends State<ProductScreen> {
final TextEditingController _controller = TextEditingController();
@override
void initState() {
super.initState();
// Provider.of<ProductListViewModel>(context, listen: false).fetchProducts("");
}
@override
Widget build(BuildContext context) {
final vm = Provider.of<ProductListViewModel>(context);
return Scaffold(
appBar: AppBar(
title: Text("Movies")
),
body: Container(
padding: EdgeInsets.all(10),
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: Column(children: <Widget>[
Container(
padding: EdgeInsets.only(left: 10),
decoration: BoxDecoration(
color: Colors.grey,
borderRadius: BorderRadius.circular(10)
),
child: TextField(
controller: _controller,
onSubmitted: (value) {
if(value.isNotEmpty) {
vm.fetchProducts(value);
_controller.clear();
}
},
style: TextStyle(color: Colors.white),
decoration: InputDecoration(
hintText: "Search",
hintStyle: TextStyle(color: Colors.white),
border: InputBorder.none
),
),
),
Expanded(
child: ProductList(products: vm.products))
])
)
);
}
}
class ProductScreen扩展StatefulWidget{
@凌驾
_ProductScreenState createState()=>\u ProductScreenState();
}
类_ProductScreenState扩展状态{
最终文本编辑控制器_控制器=文本编辑控制器();
@凌驾
void initState(){
super.initState();
//Provider.of(context,listen:false).fetchProducts(“”);
}
@凌驾
小部件构建(构建上下文){
final vm=Provider.of(上下文);
返回脚手架(
appBar:appBar(
标题:文本(“电影”)
),
主体:容器(
填充:边缘设置。全部(10),
宽度:MediaQuery.of(context).size.width,
高度:MediaQuery.of(context).size.height,
子项:列(子项:[
容器(
填充:仅限边缘设置(左:10),
装饰:盒子装饰(
颜色:颜色。灰色,
边界半径:边界半径。圆形(10)
),
孩子:TextField(
控制器:_控制器,
提交:(值){
if(value.isNotEmpty){
产品(价值);
_controller.clear();
}
},
样式:TextStyle(颜色:Colors.white),
装饰:输入装饰(
hintText:“搜索”,
hintStyle:TextStyle(颜色:Colors.white),
边框:InputBorder.none
),
),
),
扩大(
子项:产品列表(产品:vm.products))
])
)
);
}
}
将MaterialApp小部件包装在ChangeNotifierProvider小部件内
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (ctx) => ProductListViewModel(),
child: MaterialApp(
title: "Products",
debugShowCheckedModeBanner: false,
home: ProductScreen(),
)
);
}
}
或者,您可以按照中所述对其进行定义
另请查看此媒体以了解有关提供者、多提供者和消费者的更多信息。包装材料应用程序小部件不起作用,出现相同的错误。@SilverKappa我认为这是我的错误在create参数中使用不同的上下文变量而不是上下文我已使用ctx更新了上下文。进行了更改,仍然存在相同的错误:错误:在此ProductScreen Widget上找不到正确的提供程序您的代码在我这方面工作正常。看起来问题不在上面的代码中?@yellowgray问题是一个import语句被android studio翻译成了“C://…”结构,你完全正确。
void main() {
runApp(
ChangeNotifierProvider(
create: (context) => ProductListViewModel(),
child: MyApp(),
),
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Products",
debugShowCheckedModeBanner: false,
home: ProductScreen(),
);
}
}