如何解决Flutter:使用ProxyProvider传递数据时发生错误
我正在尝试构建一个类似于转换器网站的应用程序,在该网站中,您需要在文本字段中放置一个链接,但是除了转换它,您只能看到该信息。我使用ProxyProvider连接2个提供程序。我遵循了本教程(https://dev.to/paulhalliday/how-to-use-proxyprovider-with-flutter-3ifo)中有关如何使用ProxyProvider并出现错误的说明。 这是代码。 -main.dart
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,theme: ThemeData(
primarySwatch: Colors.blue,textTheme: ThemeData.light().textTheme.copyWith(
headline1: TextStyle(
fontSize: 16.0,fontWeight: FontWeight.bold,color: Colors.white,),headline2: TextStyle(
fontSize: 16.0,bodyText1: TextStyle(fontSize: 18.0,fontWeight: FontWeight.w500),bodyText2: TextStyle(
fontSize: 20.0,title: 'ID',home: MultiProvider(
providers: [
Provider(
create: (context) => InputService(),ProxyProvider<InputService,Future<CoreData>>(
update: (context,InputService inputService,Future<CoreData> questionsService) =>
QuestionsService().fetchQuestion(inputService),],child: Userinput(),);
}
}
-TextField代码
class UserInput extends StatelessWidget {
@override
Widget build(BuildContext context) {
ScreenConfig().init(context);
final TextEditingController _controller = TextEditingController();
return Scaffold(
body: Container(
height: double.infinity,padding: EdgeInsets.symmetric(horizontal: 8.0),color: Colors.blueAccent,child: Column(
mainAxisAlignment: MainAxisAlignment.center,children: [
Text(
'Enter ID here',style: Theme.of(context).textTheme.bodyText2,SizedBox(height: screenHeight(0.05),TextField(
controller: _controller,decoration: Inputdecoration(
hintText: 'ID',hintStyle: TextStyle(color: Colors.grey),fillColor: Colors.white,filled: true,border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),SizedBox(height: 10.0,RaisedButton(
child: Text(
'Enter',shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0),color: Colors.lightBlue,onpressed: () {
GetInput(userInput: _controller.text);
print(GetInput(userInput: _controller.text));
Provider.of<InputService>(context,listen: false);
Navigator.pushReplacement(context,MaterialPageRoute(builder: (context) => HomePage()));
},);
}
}
class HomePage extends StatelessWidget {
Widget build(BuildContext context) {
Future<CoreData> question = Provider.of<Future<CoreData>>(context);
return FutureProvider(
create: (BuildContext context) => question,child: Scaffold(
backgroundColor: Colors.grey[100],body: question == null
? Center(child: CircularProgressIndicator())
: CustomScrollView(
slivers: [
CustomAppBar(),CustomList(),);
}
}
class GetInput {
String userInput;
GetInput({@required this.userInput});
}
class InputService {
GetInput getInput;
}
class QuestionsService {
Future<CoreData> fetchQuestion(InputService inputService) async {
final response = await http.get(inputService.getInput.userInput);
var jsonResponse = convert.jsonDecode(response.body);
CoreData parsedQuestions = CoreData.fromJson(jsonResponse);
return parsedQuestions;
}
}
-错误消息
I/Flutter (21927): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/Flutter (21927): The following ProviderNotFoundException was thrown building HomePage(dirty):
I/Flutter (21927): Error: Could not find the correct Provider<Future<CoreData>> above this HomePage Widget
I/Flutter (21927):
I/Flutter (21927): This likely happens because you used a `BuildContext` that does not include the provider
I/Flutter (21927): of your choice. There are a few common scenarios:
I/Flutter (21927):
I/Flutter (21927): - The provider you are trying to read is in a different route.
I/Flutter (21927):
I/Flutter (21927): Providers are "scoped". So if you insert of provider inside a route,then
I/Flutter (21927): other routes will not be able to access that provider.
I/Flutter (21927):
I/Flutter (21927): - You used a `BuildContext` that is an ancestor of the provider you are trying to read.
I/Flutter (21927):
I/Flutter (21927): Make sure that HomePage is under your MultiProvider/Provider<Future<CoreData>>.
I/Flutter (21927): This usually happen when you are creating a provider and trying to read it immediately.
I/Flutter (21927):
I/Flutter (21927): For example,instead of:
I/Flutter (21927):
I/Flutter (21927): ```
I/Flutter (21927): Widget build(BuildContext context) {
I/Flutter (21927): return Provider<Example>(
I/Flutter (21927): create: (_) => Example(),I/Flutter (21927): // Will throw a ProviderNotFoundError,because `context` is associated
I/Flutter (21927): // to the widget that is the parent of `Provider<Example>`
I/Flutter (21927): child: Text(context.watch<Example>()),I/Flutter (21927): ),I/Flutter (21927): }
I/Flutter (21927): ```
I/Flutter (21927):
I/Flutter (21927): consider using `builder` like so:
I/Flutter (21927):
I/Flutter (21927): ```
I/Flutter (21927): Widget build(BuildContext context) {
I/Flutter (21927): return Provider<Example>(
I/Flutter (21927): create: (_) => Example(),I/Flutter (21927): // we use `builder` to obtain a new `BuildContext` that has access to the provider
I/Flutter (21927): builder: (context) {
I/Flutter (21927): // No longer throws
I/Flutter (21927): return Text(context.watch<Example>()),I/Flutter (21927): }
I/Flutter (21927): ),I/Flutter (21927): }
I/Flutter (21927): ```
I/Flutter (21927):
I/Flutter (21927): If none of these solutions work,consider asking for help on StackOverflow:
I/Flutter (21927): https://stackoverflow.com/questions/tagged/Flutter
I/Flutter (21927):
I/Flutter (21927): The relevant error-causing widget was:
I/Flutter (21927): HomePage
I/Flutter (21927): file:///C:/Users/bryan/source/repos/Flutter%20Projects/testing/quizizz_cheat/lib/screens/user_input.dart:52:92
I/Flutter (21927):
I/Flutter (21927): When the exception was thrown,this was the stack:
I/Flutter (21927): #0 Provider._inheritedElementOf (package:provider/src/provider.dart:309:7)
I/Flutter (21927): #1 Provider.of (package:provider/src/provider.dart:261:30)
I/Flutter (21927): #2 HomePage.build (package:quiz/screens/home.dart:9:42)
I/Flutter (21927): #3 StatelessElement.build (package:Flutter/src/widgets/framework.dart:4644:28)
I/Flutter (21927): #4 ComponentElement.performRebuild (package:Flutter/src/widgets/framework.dart:4570:15)
I/Flutter (21927): #5 Element.rebuild (package:Flutter/src/widgets/framework.dart:4286:5)
I/Flutter (21927): #6 ComponentElement._firstBuild (package:Flutter/src/widgets/framework.dart:4549:5)
I/Flutter (21927): #7 ComponentElement.mount (package:Flutter/src/widgets/framework.dart:4544:5)
I/Flutter (21927): ... normal element mounting (132 frames)
I/Flutter (21927): #139 Element.inflateWidget (package:Flutter/src/widgets/framework.dart:3512:14)
I/Flutter (21927): #140 Element.updateChild (package:Flutter/src/widgets/framework.dart:3270:18)
I/Flutter (21927): #141 RenderObjectElement.updateChildren (package:Flutter/src/widgets/framework.dart:5648:32)
I/Flutter (21927): #142 MultiChildRenderObjectElement.update (package:Flutter/src/widgets/framework.dart:6189:17)
I/Flutter (21927): #143 Element.updateChild (package:Flutter/src/widgets/framework.dart:3257:15)
I/Flutter (21927): #144 ComponentElement.performRebuild (package:Flutter/src/widgets/framework.dart:4595:16)
I/Flutter (21927): #145 StatefulElement.performRebuild (package:Flutter/src/widgets/framework.dart:4743:11)
I/Flutter (21927): #146 Element.rebuild (package:Flutter/src/widgets/framework.dart:4286:5)
I/Flutter (21927): #147 StatefulElement.update (package:Flutter/src/widgets/framework.dart:4775:5)
I/Flutter (21927): #148 Element.updateChild (package:Flutter/src/widgets/framework.dart:3257:15)
I/Flutter (21927): #149 ComponentElement.performRebuild (package:Flutter/src/widgets/framework.dart:4595:16)
I/Flutter (21927): #150 Element.rebuild (package:Flutter/src/widgets/framework.dart:4286:5)
I/Flutter (21927): #151 ProxyElement.update (package:Flutter/src/widgets/framework.dart:4930:5)
I/Flutter (21927): #152 _InheritednotifierElement.update (package:Flutter/src/widgets/inherited_notifier.dart:183:11)
I/Flutter (21927): #153 Element.updateChild (package:Flutter/src/widgets/framework.dart:3257:15)
I/Flutter (21927): #154 SingleChildRenderObjectElement.update (package:Flutter/src/widgets/framework.dart:6068:14)
I/Flutter (21927): #155 Element.updateChild (package:Flutter/src/widgets/framework.dart:3257:15)
I/Flutter (21927): #156 ComponentElement.performRebuild (package:Flutter/src/widgets/framework.dart:4595:16)
I/Flutter (21927): #157 StatefulElement.performRebuild (package:Flutter/src/widgets/framework.dart:4743:11)
I/Flutter (21927): #158 Element.rebuild (package:Flutter/src/widgets/framework.dart:4286:5)
I/Flutter (21927): #159 StatefulElement.update (package:Flutter/src/widgets/framework.dart:4775:5)
I/Flutter (21927): #160 Element.updateChild (package:Flutter/src/widgets/framework.dart:3257:15)
I/Flutter (21927): #161 SingleChildRenderObjectElement.update (package:Flutter/src/widgets/framework.dart:6068:14)
I/Flutter (21927): #162 Element.updateChild (package:Flutter/src/widgets/framework.dart:3257:15)
I/Flutter (21927): #163 SingleChildRenderObjectElement.update (package:Flutter/src/widgets/framework.dart:6068:14)
I/Flutter (21927): #164 Element.updateChild (package:Flutter/src/widgets/framework.dart:3257:15)
I/Flutter (21927): #165 ComponentElement.performRebuild (package:Flutter/src/widgets/framework.dart:4595:16)
I/Flutter (21927): #166 Element.rebuild (package:Flutter/src/widgets/framework.dart:4286:5)
I/Flutter (21927): #167 StatelessElement.update (package:Flutter/src/widgets/framework.dart:4651:5)
I/Flutter (21927): #168 Element.updateChild (package:Flutter/src/widgets/framework.dart:3257:15)
I/Flutter (21927): #169 ComponentElement.performRebuild (package:Flutter/src/widgets/framework.dart:4595:16)
I/Flutter (21927): #170 Element.rebuild (package:Flutter/src/widgets/framework.dart:4286:5)
I/Flutter (21927): #171 ProxyElement.update (package:Flutter/src/widgets/framework.dart:4930:5)
I/Flutter (21927): #172 Element.updateChild (package:Flutter/src/widgets/framework.dart:3257:15)
I/Flutter (21927): #173 ComponentElement.performRebuild (package:Flutter/src/widgets/framework.dart:4595:16)
I/Flutter (21927): #174 StatefulElement.performRebuild (package:Flutter/src/widgets/framework.dart:4743:11)
I/Flutter (21927): #175 Element.rebuild (package:Flutter/src/widgets/framework.dart:4286:5)
I/Flutter (21927): #176 BuildOwner.buildScope (package:Flutter/src/widgets/framework.dart:2673:33)
I/Flutter (21927): #177 WidgetsBinding.drawFrame (package:Flutter/src/widgets/binding.dart:913:20)
I/Flutter (21927): #178 RendererBinding._handlePersistentFrameCallback (package:Flutter/src/rendering/binding.dart:302:5)
I/Flutter (21927): #179 SchedulerBinding._invokeFrameCallback (package:Flutter/src/scheduler/binding.dart:1117:15)
I/Flutter (21927): #180 SchedulerBinding.handleDrawFrame (package:Flutter/src/scheduler/binding.dart:1055:9)
I/Flutter (21927): #181 SchedulerBinding._handleDrawFrame (package:Flutter/src/scheduler/binding.dart:971:5)
I/Flutter (21927): #185 _invoke (dart:ui/hooks.dart:251:10)
I/Flutter (21927): #186 _drawFrame (dart:ui/hooks.dart:209:3)
I/Flutter (21927): (elided 3 frames from dart:async)
I/Flutter (21927):
I/Flutter (21927): ════════════════════════════════════════════════════════════════════════════════════════════════════
解决方法
错误告诉您Provider已“作用域”。因此,如果您在路由内插入提供商,则其他路由将无法访问该提供商。您需要使提供程序可以被使用它的所有小部件访问。与其将MultiProvider
放在MaterialApp
内,不如将其向上移动。 (将其放在MaterialApp
上方)
尝试这样做:
MultiProvider(
providers: [
Provider(
create: (context) => InputService(),),ProxyProvider<InputService,Future<CoreData>>(
update: (context,InputService inputService,Future<CoreData> questionsService) =>
QuestionsService().fetchQuestion(inputService),],child: MaterialApp(...
),
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。