Provider 问题:找不到正确的 Provider<Auth>
Problem with Provider: Could not find the correct Provider<Auth>
我一直遇到这个问题:
未处理的异常:错误:在此注册小部件上方找不到正确的提供者
由于我正在尝试实现用户身份验证,因此存在此错误。
这是我的代码:
import 'package:flutter/material.dart';
import 'konto.dart';
import 'package:studyIT/providers/auth.dart';
import 'package:provider/provider.dart';
class Register extends StatefulWidget {
@override
_RegisterAnsichtState createState() => _RegisterAnsichtState();
}
class _RegisterAnsichtState extends State<Register> {
final _formKey = GlobalKey<FormState>();
Map<String, String> _authData = {
'email': '',
'passwort': '',
};
var _isLoading = false;
Future<void> _submit() async {
if (!_formKey.currentState.validate()) {
// Invalid!
return;
}
_formKey.currentState.save();
setState(() {
_isLoading = true;
});
await Provider.of<Auth>(context, listen: false).registrieren(
_authData['email'],
_authData['password'],
);
setState(() {
_isLoading = false;
});
}
@override
Widget build(BuildContext build) {
return Scaffold(body: Builder(builder: (BuildContext context) {
return Form(
key: _formKey,
child: Padding(
padding: const EdgeInsets.all(50),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
TextFormField(
onChanged: (value) {
_authData['email'] = value;
},
validator: (value) {
if (value.isEmpty ||
!value.contains('@student.tgm.ac.at')) {
return "Ungültig";
}
},
keyboardType: TextInputType.text,
autocorrect: false,
decoration: InputDecoration(
labelText: 'E-Mail',
border: OutlineInputBorder(),
),
),
TextFormField(
decoration: InputDecoration(
labelText: 'Passwort',
border: OutlineInputBorder(),
),
obscureText: true,
onChanged: (value) {
_authData['passwort'] = value;
},
validator: (value) {
if (value.isEmpty || value.length < 8) {
return "Zu kurz";
}
},
),
TextFormField(
decoration: InputDecoration(
labelText: 'Passwort wiederholen',
border: OutlineInputBorder(),
),
obscureText: true,
validator: (value) {
if (value != _authData['passwort']) {
return "Ungültige Passwörter";
}
}),
SizedBox(width: 25),
RaisedButton(
color: Colors.blue,
textColor: Colors.white,
child: Text('Registrieren'),
onPressed: () => _submit(),
// {addName(_name)},
)
],
),
));
}));
}
}
主要 class 在这里:
import 'package:flutter/material.dart';
import 'package:studyIT/RegisterAnsicht.dart';
import 'package:studyIT/providers/auth.dart';
import 'package:provider/provider.dart';
import 'package:studyIT/providers/test.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Register(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (BuildContext context) {
Auth();
},
child: MaterialApp(
home: Register(),
));
}
}
我不知道如何解决这个错误?任何时候我尝试一些我不工作的东西。
这是因为你的MyHomePage()
class没有在你的main.dart
中说明。实际上你可以摆脱这个 class (因为它没有做任何工作)并在 main.dart
中用 ChangeNotifierProvider
包装 class Register()
我一直遇到这个问题:
未处理的异常:错误:在此注册小部件上方找不到正确的提供者
由于我正在尝试实现用户身份验证,因此存在此错误。
这是我的代码:
import 'package:flutter/material.dart';
import 'konto.dart';
import 'package:studyIT/providers/auth.dart';
import 'package:provider/provider.dart';
class Register extends StatefulWidget {
@override
_RegisterAnsichtState createState() => _RegisterAnsichtState();
}
class _RegisterAnsichtState extends State<Register> {
final _formKey = GlobalKey<FormState>();
Map<String, String> _authData = {
'email': '',
'passwort': '',
};
var _isLoading = false;
Future<void> _submit() async {
if (!_formKey.currentState.validate()) {
// Invalid!
return;
}
_formKey.currentState.save();
setState(() {
_isLoading = true;
});
await Provider.of<Auth>(context, listen: false).registrieren(
_authData['email'],
_authData['password'],
);
setState(() {
_isLoading = false;
});
}
@override
Widget build(BuildContext build) {
return Scaffold(body: Builder(builder: (BuildContext context) {
return Form(
key: _formKey,
child: Padding(
padding: const EdgeInsets.all(50),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
TextFormField(
onChanged: (value) {
_authData['email'] = value;
},
validator: (value) {
if (value.isEmpty ||
!value.contains('@student.tgm.ac.at')) {
return "Ungültig";
}
},
keyboardType: TextInputType.text,
autocorrect: false,
decoration: InputDecoration(
labelText: 'E-Mail',
border: OutlineInputBorder(),
),
),
TextFormField(
decoration: InputDecoration(
labelText: 'Passwort',
border: OutlineInputBorder(),
),
obscureText: true,
onChanged: (value) {
_authData['passwort'] = value;
},
validator: (value) {
if (value.isEmpty || value.length < 8) {
return "Zu kurz";
}
},
),
TextFormField(
decoration: InputDecoration(
labelText: 'Passwort wiederholen',
border: OutlineInputBorder(),
),
obscureText: true,
validator: (value) {
if (value != _authData['passwort']) {
return "Ungültige Passwörter";
}
}),
SizedBox(width: 25),
RaisedButton(
color: Colors.blue,
textColor: Colors.white,
child: Text('Registrieren'),
onPressed: () => _submit(),
// {addName(_name)},
)
],
),
));
}));
}
}
主要 class 在这里:
import 'package:flutter/material.dart';
import 'package:studyIT/RegisterAnsicht.dart';
import 'package:studyIT/providers/auth.dart';
import 'package:provider/provider.dart';
import 'package:studyIT/providers/test.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Register(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (BuildContext context) {
Auth();
},
child: MaterialApp(
home: Register(),
));
}
}
我不知道如何解决这个错误?任何时候我尝试一些我不工作的东西。
这是因为你的MyHomePage()
class没有在你的main.dart
中说明。实际上你可以摆脱这个 class (因为它没有做任何工作)并在 main.dart
ChangeNotifierProvider
包装 class Register()