balta-io / 7186 Goto Github PK
View Code? Open in Web Editor NEWApps Nativas e Escaláveis com Flutter, BloC e Provider
Apps Nativas e Escaláveis com Flutter, BloC e Provider
Olá!
Estou seguindo a implementação do curso, porém, me deparei com um problema.
Ao clicar em "Autentique-se", presente na account.page.dart (unauthenticated-user-card.widget.dart), informar o usuário para autenticação e clicar em "Entrar", estou recebendo o seguinte erro:
Unhandled Exception: 'package:provider/src/provider.dart': Failed assertion: line 191 pos 7: 'context.owner.debugBuilding || listen == false || debugIsInInheritedProviderUpdate': Tried to listen to a value exposed with provider, from outside of the widget tree.
This is likely caused by an event handler (like a button's onPressed) that called
Provider.of without passing `listen: false`.
To fix, write:
Provider.of<UserBloc>(context, listen: false);
It is unsupported because may pointlessly rebuild the widget associated to the
event handler, when the widget tree doesn't care about the value.
Caso eu aplique a solução citada acima, adicionar o "listen: false", o projeto não acusa mais o erro, porém, não atualiza o widget da account.page.dart ao ser redirecionado com o usuário autenticado:
Método authenticate() da login.page.dart:
authenticate(BuildContext context) async {
var bloc = Provider.of<UserBloc>(context, listen: false);
var user = await bloc.authenticate(
new AuthenticateModel(
username: username,
password: password,
),
);
if (user != null) {
Navigator.pop(context);
return;
}
final snackBar = SnackBar(content: Text('Usuário ou senha invalidos'));
_scaffoldKey.currentState.showSnackBar(snackBar);
}
AccountPage
class AccountPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
var bloc = Provider.of<UserBloc>(context);
return Scaffold(
appBar: AppBar(
actions: <Widget>[
FlatButton(
child: Icon(
Icons.settings,
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SettingsPage(),
),
);
},
)
],
),
body: bloc.user == null
? UnauthenticatedUserCard()
: AuthenticatedUserCard(),
);
}
}
Segue trecho da botão da login.page.dart, e main.dart:
Botão para "Entrar" na login.page.dart:
FlatButton(
child: Text("Entrar"),
onPressed: () {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
authenticate(context);
}
},
),
main.dart
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider<HomeBloc>.value(
value: HomeBloc(),
),
ChangeNotifierProvider<CartBloc>.value(
value: CartBloc(),
),
ChangeNotifierProvider<UserBloc>.value(
value: UserBloc(),
),
ChangeNotifierProvider<ThemeBloc>.value(
value: ThemeBloc(),
),
],
child: Main(),
);
}
}
No pubspec.yaml estou utilizando: "provider: any"
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.