问题描述
如何在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',keyboardType: TextInputType.emailAddress,validator: (value) => value.isEmpty ? 'Email cannot be empty!': null,onChanged: (value) {
setState(() => email = value);
},TextFormField(
decoration: Inputdecoration(
hintText: 'Number Phone',keyboardType: TextInputType.number,validator: (value) => value.isEmpty ? 'Number Phone cannot be empty!': null,onChanged: (value) {
setState(() => phone = value);
},TextFormField(
decoration: Inputdecoration(
hintText: 'Unique ID ',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),child: Text(
snapshot.data[index].data["email"]),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: () {
}
),)
),);
});
}
}),);
}
}
然后erorr显示,在96:101行中,我将nagivator.push的Iconbutton推送到UpdateSupervisor。 所以我的问题是如何在onpressed()中包装流提供程序?
错误:在此UpdateSupervisor小部件上方找不到正确的提供程序
要解决,请:
- 确保提供者是此UpdateSupervisor小部件的祖先
- 向提供者提供类型
- 为消费者提供类型
- 将类型提供给Provider.of()
- 始终使用包导入。例如:`import'package:my_app / my_code.dart';
- 确保使用正确的
context
。
如果这些解决方案均无效,请在以下位置提交错误: https://github.com/rrousselGit/provider/issues 相关的引起错误的小部件是: UpdateSupervisor文件:/// D:/Android_project/finalyearproject/lib/screen/home/HomeSupervisor.dart:96:101
解决方法
使用这样的提供程序包装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',)
这将使您可以将 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 ,可让您将MaterialApp与多个提供程序包装在一起。
还要检查此url
来自flutter.dev 的 MultiProvider 的示例
void main() {
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => CartModel()),Provider(create: (context) => SomeOtherClass()),],child: MyApp(),);
}