问题描述
HomeSupervisor类
import 'package:Flutter/material.dart';
import 'package:fyp/provider/supervisorProvider.dart';
import 'package:fyp/screen/AddSupervisor.dart';
import 'package:fyp/sidebar/AdminDrawer.dart';
import 'package:provider/provider.dart';
class HomeSupervisor extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => SupervisorProvider(),child: Scaffold(
appBar: AppBar(
title: Text('Supervisor'),actions: <Widget>[
IconButton(
icon: Icon(
Icons.add,color: Colors.white,),onpressed:(){
Navigator.push(context,MaterialPageRoute(builder: (context) => AddSupervisor()));
})
],drawer: AdminDrawer(),);
}
}
AddSupervisor类
import 'package:Flutter/material.dart';
import 'package:fyp/provider/supervisorProvider.dart';
import 'package:provider/provider.dart';
class AddSupervisor extends StatefulWidget {
@override
_AddSupervisorState createState() => _AddSupervisorState();
}
class _AddSupervisorState extends State<AddSupervisor> {
bool loading = false;
@override
Widget build(BuildContext context) {
final supervisorProvider = Provider.of<SupervisorProvider>(context);
return Scaffold(
appBar: AppBar(
title: Text('Supervisor'),body: SingleChildScrollView(
child: Form(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,children: <Widget>[
SizedBox(height: 10.0),TextFormField(
decoration: Inputdecoration(labelText: 'Name',border: OutlineInputBorder()),keyboardType: TextInputType.text,validator: (value) => value.isEmpty ? 'Enter a name': null,onChanged: (value) {
supervisorProvider.changeName(value);
},SizedBox(height: 5.0),TextFormField(
decoration: Inputdecoration(labelText: 'Email',keyboardType: TextInputType.emailAddress,validator: (value) => value.isEmpty ? 'Enter a email': null,onChanged: (value) {
supervisorProvider.changeEmail(value);
},TextFormField(
decoration: Inputdecoration(labelText: 'Number Phone',keyboardType: TextInputType.number,validator: (value) => value.isEmpty ? 'Enter a Phone': null,onChanged: (value) {
supervisorProvider.changePhone(value);
},TextFormField(
decoration: Inputdecoration(labelText: 'Ic Number ',validator: (value) => value.isEmpty ? 'Enter a ic number': null,onChanged: (value) {
supervisorProvider.changeIcNumber(value);
},SizedBox(height: 10.0),RaisedButton(
color: Colors.grey,textColor: Colors.black,child: Text("Submit"),onpressed:(){
supervisorProvider.addSupervisor();
Navigator.of(context).pop();
}
),],);
}
}
SupervisorProvider类
import 'package:Flutter/cupertino.dart';
import 'package:fyp/model/Supervisor.dart';
import 'package:fyp/service/database.dart';
import 'package:uuid/uuid.dart';
class SupervisorProvider with ChangeNotifier{
final DatabaseService db = DatabaseService();
String _name;
String _email;
String _icNumber;
String _numberphone;
var uuid = Uuid();
// getters
String get name => _name;
String get email => _email;
String get icNo => _icNumber;
String get nophone => _numberphone;
// setters
changeName(String value){
_name=value;
notifyListeners();
}
changeEmail(String value){
_email=value;
notifyListeners();
}
changePhone(String value){
_numberphone=value;
notifyListeners();
}
changeIcNumber(String value){
_icNumber=value;
notifyListeners();
}
addSupervisor(){
var NewSupervisor = Supervisor(name: name,email: email,icNo: icNo,nophone: nophone,supervisorId: uuid.v4());
db.addSupervisor(NewSupervisor);
}
}
然后我得到了这样的错误
错误:在此AddSupervisor小部件上方找不到正确的提供程序
这可能是因为您使用的BuildContext
不包含提供商
你的选择。有几种常见情况:
-
您尝试读取的提供程序处于不同的路径。
提供者是“范围”的。因此,如果您将提供商插入路由内,则 其他路由将无法访问该提供商。
-
您使用的
BuildContext
是您尝试读取的提供程序的祖先。确保AddSupervisor在您的MultiProvider / Provider下。 当您创建提供程序并尝试立即读取它时,通常会发生这种情况。
例如,代替:
小部件构建(BuildContext上下文){ 返回提供者( 创建:(_)=> Example(), //将抛出ProviderNotFoundError,因为
context
已关联 //指向Provider<Example>
父级的小部件 子级:Text(context.watch()), ), }考虑像这样使用构建器:
小部件构建(BuildContext上下文){ 返回提供者( 创建:(_)=> Example(), //我们使用
builder
获得可以访问提供者的新BuildContext
生成器:(上下文){ //不再抛出 返回Text(context.watch()), } ), }
如果这些解决方案都不起作用,请考虑在StackOverflow上寻求帮助: https://stackoverflow.com/questions/tagged/flutter 相关的引起错误的小部件是: AddSupervisor文件:/// D:/Android_project/fyp/lib/screen/home/HomeSupervisor.dart:20:83
我有什么想念的吗?我需要别人的帮助
解决方法
如果要在提供程序中使用某些类,则必须向提供程序提供一个类实例,
例如我这样做
class App extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
Provider<ThemeStore>(
create: (_) => ThemeStore(),),Provider<AuthStore>(
create: (_)=> AuthStore(),],builder: (context,w){
return MaterialApp(
title: "App",theme: context.watch<ThemeStore>().selectedThemeData,initialRoute: "/",onGenerateRoute: RouteManager.onGenerate,);
},);
}
}
因此,在小部件树的下方,我可以使用Provider.of(context)
class SearchingScreen extends StatelessWidget {
AuthBase authBase = AuthBase();
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (_) => UserModel(),child:HomeView(),);
} }