错误:在此 UpdateSupervisor 小部件上方找不到正确的 Provider<NewUser>
Posted
技术标签:
【中文标题】错误:在此 UpdateSupervisor 小部件上方找不到正确的 Provider<NewUser>【英文标题】:Error: Could not find the correct Provider<NewUser> above this UpdateSupervisor Widget 【发布时间】:2021-01-31 21:04:22 【问题描述】:如何将流提供程序包装在 onPressed 函数中?这是我的课。
UpdateSupervisor 类
import 'package:finalyearproject/model/NewUser.dart';
import 'package:finalyearproject/service/database.dart';
import 'package:finalyearproject/shared/Loading.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class UpdateSupervisor extends StatefulWidget
@override
_UpdateSupervisorState createState() => _UpdateSupervisorState();
class _UpdateSupervisorState extends State<UpdateSupervisor>
// form values
String name;
String email;
String uniqueID;
String phone;
String id;
final GlobalKey<FormState> _formKey = GlobalKey();
@override
Widget build(BuildContext context)
final user = Provider.of<NewUser>(context);
return Scaffold(
appBar: AppBar(
title: Text('Edit Supervisor'),
backgroundColor: Colors.redAccent,
),
body: StreamBuilder(
stream: DatabaseService(uid: user.id).userData,
builder: (context, snapshot)
if(!snapshot.hasData)
return Loading();
NewUser userData = snapshot.data;
return Form(
key: _formKey,
child: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
SizedBox(height: 25.0),
TextFormField(
decoration: InputDecoration(
hintText: 'Name',
border: OutlineInputBorder(borderRadius: BorderRadius.circular(5))),
keyboardType: TextInputType.text,
initialValue: userData.name,
validator: (value) => value.isEmpty ? 'Name cannot be empty!': null,
onChanged: (value)
setState(() => name = value);
,
),
SizedBox(height: 10.0),
TextFormField(
decoration: InputDecoration(
hintText: 'Email',
border: OutlineInputBorder(borderRadius: BorderRadius.circular(5))),
keyboardType: TextInputType.emailAddress,
validator: (value) => value.isEmpty ? 'Email cannot be empty!': null,
onChanged: (value)
setState(() => email = value);
,
),
SizedBox(height: 10.0),
TextFormField(
decoration: InputDecoration(
hintText: 'Number Phone',
border: OutlineInputBorder(borderRadius: BorderRadius.circular(5))),
keyboardType: TextInputType.number,
validator: (value) => value.isEmpty ? 'Number Phone cannot be empty!': null,
onChanged: (value)
setState(() => phone = value);
,
),
SizedBox(height: 10.0),
TextFormField(
decoration: InputDecoration(
hintText: 'Unique ID ',
border: OutlineInputBorder(borderRadius: BorderRadius.circular(5))),
keyboardType: TextInputType.number,
validator: (value) => value.isEmpty ? 'Ic number cannot be empty!': null,
onChanged: (value)
setState(() => uniqueID = value);
,
),
const SizedBox(height: 20.0),
RaisedButton(
color: Colors.redAccent,
textColor: Colors.black,
child: Text("Update"),
onPressed: () async
if(_formKey.currentState.validate())
_formKey.currentState.save();
else
print("Validator are correct!");
),
],
),
),
);
)
);
HomeSupervisor 类
import 'package:flutter/material.dart';
import 'package:finalyearproject/model/NewUser.dart';
import 'package:finalyearproject/screen/crud/UpdateSupervisor.dart';
import 'package:finalyearproject/screen/crud/AddSupervisor.dart';
import 'package:finalyearproject/service/database.dart';
import 'package:finalyearproject/sidebar/AdminDrawer.dart';
class HomeSupervisor extends StatelessWidget
@override
Widget build(BuildContext context)
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.redAccent,
title: Text('Supervisor'),
actions: <Widget>[
IconButton(
icon: Icon(
Icons.add,
color: Colors.white,
),
onPressed: ()
Navigator.push(context,
MaterialPageRoute(builder: (context) => AddSupervisor()));
)
],
),
drawer: AdminDrawer(),
body: ListSupervisor(),
);
class ListSupervisor extends StatefulWidget
@override
_ListSupervisorState createState() => _ListSupervisorState();
class _ListSupervisorState extends State<ListSupervisor>
NewUser user;
@override
Widget build(BuildContext context)
return Container(
child: FutureBuilder(
future: DatabaseService().getPost(),
builder: (_, snapshot)
if (snapshot.connectionState == ConnectionState.waiting)
return Center(
child: Text("Loading..."),
);
else
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (_, index)
return Card(
child: ListTile(
title: Container(
alignment: Alignment.centerLeft,
child: Column(
children: <Widget>[
SizedBox(height: 5.0),
Container(alignment: Alignment.centerLeft,
child: Text(
snapshot.data[index].data["name"]),
),
SizedBox(height: 5.0),
Container(alignment: Alignment.centerLeft,
child: Text(
snapshot.data[index].data["email"]),
),
SizedBox(height: 5.0),
Container(alignment: Alignment.centerLeft,
child: Text(
snapshot.data[index].data["uniqueID"]),
),
],
),
),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(
icon: Icon(Icons.delete),
color: Colors.red,
onPressed: ()
),
IconButton(
icon: Icon(Icons.edit),
color: Colors.black,
onPressed: ()
Navigator.push(context, MaterialPageRoute(builder: (context) => UpdateSupervisor())); //here the line error show//
),
IconButton(
icon: Icon(Icons.share),
onPressed: ()
),
],
)
),
);
);
),
);
然后错误显示,在第 96:101 行中,我将 nagivator.push 到 UpdateSupervisor 的 Iconbutton。 所以我的问题是如何将流提供程序包装在 onPressed() 中?
错误:在此 UpdateSupervisor 小部件上方找不到正确的提供程序
要修复,请:
确保 Provider 是此 UpdateSupervisor 小部件的祖先 向 Provider 提供类型 向消费者提供类型 向 Provider.of() 提供类型 始终使用包导入。例如:`import 'package:my_app/my_code.dart'; 确保使用了正确的context
。
如果这些解决方案都不起作用,请在以下位置提交错误: https://github.com/rrousselGit/provider/issues 相关的导致错误的小部件是: UpdateSupervisor file:///D:/Android_project/finalyearproject/lib/screen/home/HomeSupervisor.dart:96:101
【问题讨论】:
谁能帮帮我=( 【参考方案1】:像这样用提供者包装 MaterialApp。
ChangeNotifierProvider<NewUser>(create: (context) => NewUser(),
child: MaterialApp(
title: 'Flutter Demo',
home:Home(),
),
);
另外: 我正在使用 ChangeNotifierProvider,因为我假设您的提供程序正在扩展 ChangeNotifier。
如果您不扩展 ChangeNotifier 您可以使用以下基本形式的提供程序:
Provider<NewUser>(
create: (context) => NewUser(),
child: child: MaterialApp(
title: 'Flutter Demo',
home:Home(),
),
)
这将使您可以使用 NewUser 对象作为所有 Provider 后代类的依赖注入,如下所示:
class HomeWidget extends StatelessWidget
@override
Widget buid(BuildContext context)
final provider = Provider.of<NewUser>(contextm, listen:false); /// you can to not set listen on false it does not matter in this case cause this type of provider is just an injection
return Text(provider.someText);
还有 StreamProvider 可以让您在流中收听并更新注入此提供程序的 Widget流将接收数据,也请检查一下。
还有一个 MultiProvider,它可以让您用多个 Provider 包装 MaterialApp。
也检查一下这个url
来自flutter.dev的MultiProvider示例
void main()
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => CartModel()),
Provider(create: (context) => SomeOtherClass()),
],
child: MyApp(),
),
);
【讨论】:
如果我的提供商没有使用 changeNotifier 扩展?还是我需要扩展? 有几种类型的提供者,这取决于你的使用情况,如果你愿意,在提供者的数据发生变化时更新一些小部件,它应该扩展 changenotifier 否则你可以使用这样的东西:Provider(create: (context) => SomeOtherClass()) 编码streamprovider怎么写?与 mutliprovider 的编码相同吗? 如果我想使用mutliprovider,它必须声明主类对吗?就我而言,我注意到它涉及许多小部件。以上是关于错误:在此 UpdateSupervisor 小部件上方找不到正确的 Provider<NewUser>的主要内容,如果未能解决你的问题,请参考以下文章
颤振:RangeError(索引):无效值:不在包含范围内0..27:28