如何解决无法在此 Widget 上方找到正确的 Provider - Flutter
我是 Flutter 的新手,我在 Flutter 应用程序中使用了 Provider,我的 main.dart 文件如下所示:
import 'package:Flutter/material.dart';
import 'package:provider_practice/screens/first_screen.dart';
import 'package:provider_practice/screens/second_screen.dart';
import 'package:provider/provider.dart';
import 'package:provider_practice/counter.dart';
void main() {
runApp(MaterialApp(
home: ChangeNotifierProvider<Counter>(
create: (context) => Counter(),child: FirstScreen(),),routes: {
"/first" : (context) => FirstScreen(),"/second" : (context) => SecondScreen(),},));
}
我的代码在 FirstScreen() 上运行良好,但是当我导航到 SecondScreen()(通过使用 Navigator.pushNamed())时,出现以下错误:
The following ProviderNotFoundException was thrown building SecondScreen(dirty):
Error: Could not find the correct Provider<Counter> above this SecondScreen Widget
有没有办法解决这个问题?我正在使用 Provider 版本 ^5.0.0
first_screen.dart :-
import 'package:Flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:provider_practice/counter.dart';
class FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
final counter = Provider.of<Counter>(context,listen: false);
return Scaffold(
appBar: AppBar(
title: Text("First Screen"),body: Padding(
padding: const EdgeInsets.all(16.0),child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,children: [
Consumer<Counter>(
builder: (_,counter,__) => Text("You pressed the button
${counter.firstCounter} times.")
),SizedBox(height: 20),RaisedButton(
onpressed: counter.firstIncrement,child: Text("Click Me"),RaisedButton(
onpressed: () {
Navigator.pushNamed(context,"/second");
},child: Text("Go to Second"),],);
}
}
second_screen.dart :-
import 'package:Flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:provider_practice/counter.dart';
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
final counter = Provider.of<Counter>(context,listen: false);
return Scaffold(
appBar: AppBar(
title: Text("Second Screen"),__) => Text("You pressed the button
${counter.secondCounter} times.")
),RaisedButton(
onpressed: counter.secondIncrement,RaisedButton(
onpressed: () {
//Navigator.maybePop(context);
Navigator.pushNamed(context,"/first");
//Navigator.pop(context);
},child: Text("Go to First"),);
}
}
计数器.dart :-
import 'package:Flutter/foundation.dart';
class Counter with ChangeNotifier {
int firstCounter = 0;
int secondCounter = 0;
void firstIncrement() {
firstCounter++;
notifyListeners();
}
void secondIncrement() {
secondCounter++;
notifyListeners();
}
}
解决方法
如果没有看到您的代码的可重现片段,我们无能为力, 无论如何,您可以在材料应用程序之前返回多提供程序并在此处添加您的提供程序
....
return MultiProvider(
providers: [
ChangeNotifierProvider<Counter>(
create: (context) => Counter()),]
child:MaterialApp
,
你可以试试这个。
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => AriticleProvider()),ChangeNotifierProvider(create: (_) => FavoriteProvider()),],child: MyApp(),),);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。