Olá, sempre que chamo algum elemento do meu Provider, recebo esse erro que esta null
import 'package:controlsport_app_ecommerce/helpers/validator.dart';
import 'package:controlsport_app_ecommerce/models/usuarios/user.dart';
import 'package:controlsport_app_ecommerce/models/usuarios/user_manager.dart';
import 'package:flutter/material.dart';
import 'package:sign_button/sign_button.dart';
import 'package:provider/provider.dart';
class LoginScreen extends StatelessWidget {
TextStyle style = TextStyle(fontFamily: 'Montserrat', fontSize: 20.0);
final GlobalKey<FormState> formkey =
GlobalKey<FormState>(); // key do formulario
final GlobalKey<ScaffoldState> scaffoldKey =
GlobalKey<ScaffoldState>(); // key Scaffold
final TextEditingController emailController = TextEditingController();
final TextEditingController senhaController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
key: scaffoldKey,
appBar: AppBar(
title: Text('Sport\'s Control'),
centerTitle: true,
),
body: Center(
child: Card(
margin: const EdgeInsets.symmetric(horizontal: 16),
child: Form(
key: formkey,
child: Consumer<UserManager>(
child: SizedBox(
height: 120.0,
child: Image.asset(
"assets/logo.png",
fit: BoxFit.contain,
),
),
builder: (_, userManager, child) {
return ListView(
padding: EdgeInsets.all(16),
shrinkWrap: true,
children: <Widget>[
child,
SizedBox(height: 20),
// Form Field Email
TextFormField(
controller: emailController,
enabled: !userManager.loading,
style: style,
decoration: InputDecoration(
hintText: 'Informe o e-mail',
contentPadding: EdgeInsets.fromLTRB(20, 15, 20, 15),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(32),
),
),
keyboardType: TextInputType.emailAddress,
autocorrect: false, // não corrige o que digitou
validator: (email) {
if (!emailValid(email)) {
return 'Email invalido';
} else {
return null;
}
},
),
const SizedBox(height: 16),
// form field senha
TextFormField(
controller: senhaController,
enabled: !userManager.loading,
style: style,
decoration: InputDecoration(
hintText: 'Senha',
contentPadding: EdgeInsets.fromLTRB(20, 15, 20, 15),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(32),
),
),
autocorrect: false, // não corrige o q digitou
obscureText: true,
validator: (senha) {
if (senha.isEmpty || senha.length < 6) {
return 'Senha inválida';
} else {
return null;
}
},
),
const SizedBox(height: 7),
Align(
alignment: Alignment.centerRight,
child: FlatButton(
padding: EdgeInsets.zero,
onPressed: () {},
child: const Text('Esqueci minha senha'),
),
),
// botão entrar
SizedBox(
height: 44,
child: RaisedButton(
onPressed: () {
if (formkey.currentState.validate()) {
userManager.fazerLogin(
usuario: Usuario(
email: emailController.text,
senha: senhaController.text),
onFail: (e) {
scaffoldKey.currentState
.showSnackBar(SnackBar(
content: Text('Falha ao entrar: $e'),
backgroundColor: Colors.red,
));
},
onSuccess: () {
// TODO: Fechar tela de login
},
);
}
},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
),
color: Theme.of(context).primaryColor,
textColor: Colors.white,
child: const Text(
'Entrar',
style: TextStyle(fontSize: 18),
),
),
),
// botaão login Google
SizedBox(height: 10),
SizedBox(
height: 44,
child: SignInButton(
btnText: 'Entrar com o Google',
buttonType: ButtonType.google,
onPressed: () {
print('click');
}),
),
// botão login git
SizedBox(height: 10),
SizedBox(
height: 44,
child: SignInButton(
buttonType: ButtonType.github,
onPressed: () {
print('click');
}),
),
// ainda n tem conta?
Align(
alignment: Alignment.center,
child: FlatButton(
padding: EdgeInsets.zero,
onPressed: () {},
child:
const Text('Não possui uma conta? Cadastre-se'),
),
),
],
);
},
)),
),
),
);
}
}
Minha variavel esta iniciada olhe o codigo abiaxo:
import 'package:controlsport_app_ecommerce/helpers/firabase_erros.dart';
import 'package:controlsport_app_ecommerce/models/usuarios/user.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart';
class UserManager extends ChangeNotifier {
// config FIREBASE
final FirebaseAuth auth = FirebaseAuth.instance;
bool loading = false;
///
////sigin
Future<void> fazerLogin(
{Usuario usuario, Function onFail, Function onSuccess}) async {
print('o valor do loading é $loading');
// resultado a auteticação
setLoading(true);
try {
final UserCredential result = await auth.signInWithEmailAndPassword(
email: usuario.email,
password: usuario.senha,
);
onSuccess();
await Future.delayed(Duration(seconds: 5));
} on FirebaseAuthException catch (e, s) {
onFail(getErrorString(e.code));
} on Exception catch (e, s) {}
setLoading(false);
}
void setLoading(bool value) {
print(' o valor q esta aq eé o $value');
loading = value;
notifyListeners();
}
bool getLoading() {
return loading;
}
}
segue o erro
════════ Exception caught by gesture ═══════════════════════════════════════════
The following NoSuchMethodError was thrown while handling a gesture:
The method 'fazerLogin' was called on null.
Receiver: null
Tried calling: fazerLogin(onFail: Closure: (dynamic) => Null, onSuccess: Closure: () => Null, usuario: Instance of 'Usuario')
When the exception was thrown, this was the stack
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1 LoginScreen.build.<anonymous closure>.<anonymous closure>
package:controlsport_app_ecommerce/…/login/login_screen.dart:113
#2 _InkResponseState._handleTap
package:flutter/…/material/ink_well.dart:990
#3 GestureRecognizer.invokeCallback
package:flutter/…/gestures/recognizer.dart:183
#4 TapGestureRecognizer.handleTapUp
package:flutter/…/gestures/tap.dart:598
...
Handler: "onTap"
Recognizer: TapGestureRecognizer#9913a
debugOwner: GestureDetector
state: ready
won arena
finalPosition: Offset(177.7, 433.5)
finalLocalPosition: Offset(145.7, 17.2)
button: 1