I'm trying to implement an authentication flow (user login, log out, autologin etc) in Flutter using AWS Amplify and there is something I can't fixed. I like to do this as clean as possible, so I'm not using third-party packages apart from the AWS ones and Provider for state management.
Autologin is the thing that is not working. I need to hot refresh the app so autologin works. (normally this points towards an state management issue)
I'm not having errors or exceptions of any kind apart from this suspicious output:
D/AWSMobileClient(30193): _federatedSignIn: Putting provider and token in store
D/AWSMobileClient(30193): Inspecting user state details
D/AWSMobileClient(30193): hasFederatedToken: false provider: cognito-idp.ap-southeast-2.amazonaws.com/ap-southeast-2_Fd5bKVAbV
Any help is welcome.
main:
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => Tracks()),
ChangeNotifierProvider(create: (_) => Player()),
ChangeNotifierProvider(create: (_) => User()),
Provider(create: (_) => SearchTracksService()),
Provider(create: (_) => AuthenticationServices()),
],
child: Builder(
builder: (context) {
Commands.init(context);
AuthenticationCommands().getCurrentUser();
//AuthenticationCommands().fetchSession();
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Music Player',
theme: theme,
home: Provider.of<User>(context, listen: false).isAuthenticated
? HomeScreen()
: AuthenticationScreen(),
routes: {
HomeScreen.routeName: (ctx) => HomeScreen(),
AuthenticationScreen.routeName: (ctx) => AuthenticationScreen(),
SignUpConfirmationScreen.routeName: (ctx) =>
SignUpConfirmationScreen(),
},
);
},
),
);
}
}
Future<void> getCurrentUser() async {
try {
var currentUser = await authenticationServices.getCurrentUser();
if (currentUser != null) {
user.id = currentUser.userId;
user.name = currentUser.username;
user.email = currentUser.username;
user.isAuthenticated = true;
return;
}
user.isAuthenticated = false;
} catch (e) {
user.isAuthenticated = false;
throw e;
}
}
Future<AuthUser?> getCurrentUser() async {
try {
return await Amplify.Auth.getCurrentUser();
} catch (e) {
print(e);
throw (e);
}
}
import 'package:flutter/foundation.dart';
class User extends ChangeNotifier {
bool _isAuthenticated = false;
late String _id;
late String _email;
late String _name;
bool get isAuthenticated {
return _isAuthenticated;
}
set isAuthenticated(bool isAuthenticated) {
_isAuthenticated = isAuthenticated;
notifyListeners();
}
String get name {
return _name;
}
set name(String name) {
_name = name;
notifyListeners();
}
String get id {
return _id;
}
set id(String id) {
_id = id;
notifyListeners();
}
String get email {
return _email;
}
set email(String email) {
_email = email;
notifyListeners();
}
}
I also got the "false provider" error message in the console of my IDE while using the Android simulator to run an Amplify/Flutter app.
For me, the user does not get signed in at all when I see those errors in the console - and I think that those two issues are related.
So I opened an issue with the repository regarding this here, you may want to watch it because it may answer this SO issue you've raised: https://github.com/aws-amplify/amplify-flutter/issues/1204
Related
i am quite new with flutter. I am trying to add a ChangeNotifierProvider into my app. I use flutter_azure_b2c to log in a user, in order to handle to login outcome I have the following code:
AzureB2C.registerCallback(B2COperationSource.POLICY_TRIGGER_INTERACTIVE,
(result) async {
if (result.reason == B2COperationState.SUCCESS) {
List<String>? subjects = await AzureB2C.getSubjects();
if (subjects != null && subjects.isNotEmpty) {
B2CAccessToken? token = await AzureB2C.getAccessToken(subjects[0]);
if (!mounted || token == null) return;
final encodedPayload = token.token.split('.')[1];
final payloadData =
utf8.fuse(base64).decode(base64.normalize(encodedPayload));
final claims = Claims.fromJson(jsonDecode(payloadData));
var m = Provider.of<LoginModel>(context);
m.logIn(claims);
}
}
});
The problem is that when it arrives to var m = Provider.of<LoginModel>(context); the execution stops with out errors without executing m.logIn(claims);, so the model is not changed and the consumer is not called.
Any idea?
This is my consumer:
class App extends StatelessWidget {
const App({super.key});
#override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => LoginModel(),
child: MaterialApp(
debugShowCheckedModeBanner: false,
theme: appTheme,
home: Consumer<LoginModel>(
builder: (context, value, child) =>
value.claims != null ? const Home() : const Login(),
)),
);
}
}
class LoginModel extends ChangeNotifier {
Claims? _claims;
logIn(Claims claims) {
_claims = claims;
notifyListeners();
}
logOut() {
_claims = null;
notifyListeners();
}
Claims? get claims => _claims;
}
My LoginWidget:
class Login extends StatefulWidget {
const Login({super.key});
#override
LoginState createState() => LoginState();
}
class LoginState extends State<Login> {
B2CConfiguration? _configuration;
checkLogin(BuildContext context) async {
List<String>? subjects = await AzureB2C.getSubjects();
if (subjects != null && subjects.isNotEmpty) {
B2CAccessToken? token = await AzureB2C.getAccessToken(subjects[0]);
if (!mounted || token == null) return;
final encodedData = token.token.split('.')[1];
final data =
utf8.fuse(base64).decode(base64.normalize(encodedData));
final claims = Claims.fromJson(jsonDecode(data));
var m = Provider.of<LoginModel>(context, listen: true);
m.logIn(claims); //<-- debugger never reaches this line
}
}
#override
Widget build(BuildContext context) {
// It is possible to register callbacks in order to handle return values
// from asynchronous calls to the plugin
AzureB2C.registerCallback(B2COperationSource.INIT, (result) async {
if (result.reason == B2COperationState.SUCCESS) {
_configuration = await AzureB2C.getConfiguration();
if (!mounted) return;
await checkLogin(context);
}
});
AzureB2C.registerCallback(B2COperationSource.POLICY_TRIGGER_INTERACTIVE,
(result) async {
if (result.reason == B2COperationState.SUCCESS) {
if (!mounted) return;
await checkLogin(context);
}
});
// Important: Remeber to handle redirect states (if you want to support
// the web platform with redirect method) and init the AzureB2C plugin
// before the material app starts.
AzureB2C.handleRedirectFuture().then((_) => AzureB2C.init("auth_config"));
const String assetName = 'assets/images/logo.svg';
final Widget logo = SvgPicture.asset(
assetName,
);
return SafeArea(
child: //omitted,
);
}
}
I opened an issue as well, but it did not help me.
Try this
var m = Provider.of<LoginModel>(context, listen: false)._claims;
You are using the Provider syntax but not doing anything really with it. You need to set it like this Provider.of<LoginModel>(context, listen: false).login(claims) and call it like this Provider.of<LoginModel>(context, listen: false)._claims;
I fixed it, moving the callback registrations from the build method to the initState method.
I am learning about StreamProviders and ChangeNotifierProvider and how to use them in a flutter app.
The problem I am having is when I create the StreamProvider in main.dart. I am getting this error
Instance member 'getAgencyTrxn' can't be accessed using static access. (Documentation)
as designated by a red line under getAgencyTrxn(). I have been following a tutorial and also some posts here but none of them quite match what I am doing.
How do I fix this error?
Here is what I have so far:
main.dart
Widget build(BuildContext context) {
Provider.debugCheckInvalidValueType = null;
globals.newTrxn = true;
return MultiProvider(
providers: [
ChangeNotifierProvider<TrxnProvider>(create: (context) => TrxnProvider()),
StreamProvider<TrxnProvider>(
create: (context) => TrxnProvider.getAgencyTrxn(),
initialData: []),
],
child: MaterialApp(
debugShowCheckedModeBanner: false,
home: LoginScreen(),
),
);
}
trxn_provider.dart
class TrxnProvider extends ChangeNotifier {
final firestoreService = FirestoreService();
String? _clientFName;
String? _clientLName;
// Getters
String? get clientFName => _clientFName;
String? get clientLName => _clientLName;
// Setters
changeclientFName(String value) {
_clientFName = value;
notifyListeners();
}
changeclientLName(String value) {
_clientLName = value;
notifyListeners();
}
loadValues(QueryDocumentSnapshot trxns) {
_clientFName = trxns['clientFName'];
_clientLName = trxns['clientLName'];
}
getAgencyTrxn() {
return firestoreService.getAgencyTrxns();
}
saveTrxn() {
if (globals.newTrxn == true) {
_trxnId = uuId.v4();
globals.newTrxn = false;
}
var newTrxn = Trxns(
clientFName: clientFName,
clientLName: clientLName);
firestoreService.saveTrxn(newTrxn);
}
deleteTrxn(String trxnId) {
firestoreService.deleteTrxn(trxnId);
}
}
firestore_service.dart
class FirestoreService {
FirebaseFirestore _db = FirebaseFirestore.instance;
Stream<QuerySnapshot> getAgencyTrxns() async* {
yield* FirebaseFirestore.instance
.collection('agency').doc(globals.agencyId)
.collection('trxns')
.where('trxnStatus', isNotEqualTo: 'Closed')
.snapshots();
}
}
I found the solution. I needed to change this
create: (context) => TrxnProvider.getAgencyTrxn()
to this
create: (context) => TrxnProvider().getAgencyTrxn()
I am new in state Management in flutter with provider package .
How many different cause for generate these types of exception and How can I fix it,
this exception was generate when getFollowing() method was called in didChangeDependencies.
Follows.dart
class Follows with ChangeNotifier{
List<Follow> _following =[];
String userid;
String token;
List<Follow> get followingUser{
return [..._following];
}
void updates(String token,String userid){
this.userid = userid;
this.token = token;
}
Future<void> getFollowing(String id) async {
final response = await http.get("${Domain.ADDRESS}/user/following/$id",headers: {"auth-token" : this.token});
final data =json.decode(response.body)["following"] as List;
List<Follow> followingData =[];
data.forEach((user){
followingData.add(Follow(
id: user["_id"],
username: user["username"],
fullname: user["fullname"],
imageUrl: user["imageUrl"],
followerCount : (user["followers"] as List).length
));
});
_following = [...followingData];
notifyListeners();
}
.........
}
Main.dart
return MultiProvider(
providers: [
ChangeNotifierProvider(
create: (ctx) => Auth(),
),
ChangeNotifierProxyProvider<Auth , Follows>(
create: (ctx)=>Follows(),
update : (context, auth, previous) => Follows()..updates(auth.token, auth.userId)
),
]
child : .......
);
FollowList.dart
class FollowList extends StatefulWidget {
static const followRoutes = "/follow-list";
final String id;
FollowList({this.id});
#override
_FollowListState createState() => _FollowListState();
}
class _FollowListState extends State<FollowList> {
bool isLoading = false;
#override
void didChangeDependencies() {
setState(() {
isLoading = true;
});
Provider.of<Follows>(context,listen: false).getFollowing(widget.id).then((_){
setState(() {
isLoading = false;
});
});
super.didChangeDependencies();
}
#override
Widget build(BuildContext context) {
List<Follow> following = Provider.of<Follows>(context,listen: false).followingUser;
return Scaffold(
appBar: AppBar(title: Text("following),),
body: isLoading ? Center(child: CircularProgressIndicator(strokeWidth: 1,))
: ListView.builder(
itemBuilder: (context, index) => UserCard(
id: following[index].id,
fullname :following[index].fullname,
username :following[index].username,
followerCount : following[index].followerCount,
imageUrl: following[index].imageUrl,
followPressed: true,
),
itemCount: following.length,
),
);
}
}
Please specify where dispose method was called for
Unhandled Exception: A Follows was used after being disposed.
E/flutter ( 8465): Once you have called dispose() on a Follows, it can no longer be used.
ChangeNotifierProxyProvider<Auth , Follows>(
create: (ctx) => Follows(),
//update : (context, auth, previous) => Follows()..updates(auth.token, auth.userId)
// You're creating a new Follow object and disposing the old one
update: (context, auth, previous) => previous..updates(auth.token, auth.userId)
),
Instead of creating a new Follows object try to update the previous one, the listen: false will keep the reference of the old object if the ChangeNotifier updates to the new value
Same problem with me.
I Bring "Future.delayed" to apply this resolved below,
Future.delayed
[/] Your MultiProvider Correct.
#override
void didChangeDependencies() {
setState(() {
isLoading = true;
});
Future.delayed(Duration(milliseconds: 300)).then((_) async {
await Provider.of<Follows>(context, listen: false)
.getFollowing(widget.id)
.then((_) {
setState(() {
isLoading = false;
});
});
});
super.didChangeDependencies();
}
Work for me.
I am very new to Flutter so I apologize for not understanding all the terminology.
I have an application that receives data from FCM, and it shows a SnackBar (using Get). All of this is working well. The problem is the 'onTap'. When I use Get.toNamed(), it responds with,
Could not find a generator for route RouteSettings("/home-screen", null) in the _WidgetsAppState.
This is my current Snackbar
void showDialog(BuildContext context, messageJson) {
print('showDialog');
try {
final data = messageJson['data'];
final notification =
data != null && data.keys.isNotEmpty ? data : messageJson['notification'];
var body = notification['body'];
var title = notification['title'];
Get.snackbar(
title,
body,
icon: Icon(Icons.chat),
shouldIconPulse: true,
onTap: (index) {
Get.toNamed('/home-screen');
//Navigator.of(context).pushNamed('/home-screen'); // <-- Didn't work either
},
isDismissible: true,
duration: Duration(seconds: 4),
);
} catch (e) {
print(e.toString());
}
}
With my Routes being setup like this.
class Routes {
static final Map<String, WidgetBuilder> _routes = {
"/home-screen": (context) => HomeScreen(),
"/home": (context) => MainTabs(),
"/login": (context) => LoginScreen(),
"/register": (context) => RegistrationScreen(),
...VendorRoute.getAll(),
};
static Map<String, WidgetBuilder> getAll() => _routes;
static WidgetBuilder getRouteByName(String name) {
if (_routes.containsKey(name) == false) {
return _routes[RouteList.homeScreen];
}
return _routes[name];
}
static Route<dynamic> generateRoute(RouteSettings settings) {
switch (settings.name) {
case RouteList.storeDetail:
return MaterialPageRoute(
builder: VendorRoute.getRoutesWithSettings(settings)[settings.name],
);
default:
return null;
}
}
}
I'm at a standstill on trying to figure out how to navigate to a page onTap()
I have tried all the other variations as well. Any help would be appreciated here. Thanks!
REF: Flutter Get Package, https://pub.dev/packages/get
Did you set routes and onGenerateRoute inside MaterialApp? Here is an example https://flutter.dev/docs/cookbook/navigation/named-routes
in Flutter -which I just recently begin to use-, I am trying to use an AMQP stream using dart_amqp: ^0.1.4 and use providers provider: ^3.1.0+1 to make the data available throughout the app.
Only after logging in I start the AMQP service.
The AMQP part works without any issues, I get the data but I never manage to use it with Providers.
main.dart
class BigBrother extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider<EventsModel>(create: (_) => EventsModel()),
ChangeNotifierProxyProvider<EventsModel, DeviceState>(
create: (_) => new DeviceState(),
update: (context, eModel, deviceState) {
deviceState.updateFromEvent(eModel.value);
},
),
],
My models in models.dart
(As seen in below code, I also tried to used StreamProvider and commented it out)
// Global data model
class DeviceState with ChangeNotifier {
Map<String, Map<String, dynamic>> state = {};
DeviceState() {
this.state['xxx'] = {};
this.state['yyy'] = {};
}
updateFromEvent(EventsItemModel event) {
if (event != null && event.type != null) {
switch (event.type) {
case 'heartbeat':
this.state[event.device][event.type] = event.createdAt;
break;
case 'metrics':
this.state[event.device][event.type] = {}
..addAll(this.state[event.device][event.type])
..addAll(event.message);
break;
}
notifyListeners();
}
}
}
class EventsModel with ChangeNotifier {
EventsItemModel value;
bool isSubscribed = false;
AMQPModel _amqp = new AMQPModel();
// final _controller = StreamController<EventsItemModel>();
EventsModel();
// Stream<EventsItemModel> get stream => _controller.stream;
_set(String jsonString) {
value = new EventsItemModel.fromJson(jsonString);
// _controller.sink.add(value); // send data to stream
// Provider.of<DeviceState>(context, listen: false).updateFromEvent(value);
notifyListeners();
}
subscribe() {
if (!this.isSubscribed) {
this.isSubscribed = true;
this._amqp.subscribeEvents(_set); // start AMQP service after login
}
}
}
So on the login.dart view, on button pressed and validating the login, I start the AMQP stream:
onPressed: () {
if (_formKey.currentState.validate()) {
print("Login button onPressed");
Provider.of<EventsModel>(context, listen: false)
.subscribe();
Navigator.pushReplacementNamed(context, Routes.live);
}
And lastly the view after successful login:
class _LivePageState extends State<LivePage> {
#override
Widget build(BuildContext context) {
DeviceState deviceState = Provider.of<DeviceState>(context);
print('#### Device state updated');
print(deviceState.state['xxx']);
In the above code, deviceState is always null.
So after trying many combination of various Providers, I am still unable to make this work.
Would be glad to have someone's insight on this.
Best regards!