admin管理员组文章数量:1291002
Overview
My goal is to make a Flutter ios app with AWS. I am facing an issue when adding a persistent settings model to users, who will each have a corresponding settings model from their chosen items, like in any normal app.
More specifically, I want to have a single settings entry called randomPhotoOffset
, which is boolean. I was advised to use Amplify and DynamoDB to store user settings because it is scalable and offers more choices, compared to just using Cognito.
My workspace and versioning
- Using Amplify Gen 1 and CLI
- amplify_flutter: ^2.6.0
- amplify_auth_cognito: ^2.5.0
- amplify_api: ^2.6.0
- amplify_datastore: ^2.6.0
- Flutter sdk: ^3.5.4 I have updated my `pubspec.yaml' and my packages are all up to date and no versioning conflict occurs
Also testing on Simulator with device iPhone 16 Pro Mac on M4 Pro
The only issue
The only issue I am facing is an error I get when I toggle a button that changes one of my settings.
Exception updating user settings: ApiOperationException { "message": "No modelProvider found", "recoverySuggestion": "Pass in a modelProvider instance while instantiating APIPlugin" }
My code
main.dart This function is called after WidgetsFlutterBinding.ensureInitialized(); and before runApp().
Future<void> configureAmplify() async {
try {
final authPlugin = AmplifyAuthCognito();
final apiPlugin = AmplifyAPI();
final dataStorePlugin =
AmplifyDataStore(modelProvider: ModelProvider.instance);
Amplify.addPlugins(
[authPlugin, dataStorePlugin, apiPlugin],
);
await Amplify.configure(amplifyConfig);
// Start DataStore to register the model provider for GraphQL operations.
await Amplify.DataStore.start();
} on Exception catch (e) {
Log.e("Error configuring Amplify: $e");
}
}
settings.dart This is the only relevant function that is called once a button is pressed. Changes occur immediately after toggling, so a save button is avoided
Future<void> _toggleRandomPhotoOffset(bool newValue) async {
setState(() {
_randomPhotoOffset = newValue;
});
try {
// Retrieve the current user's ID from Amplify Auth.
final authUser = await Amplify.Auth.getCurrentUser();
await updateUserSettings(
userId: authUser.userId,
randomPhotoOffset: newValue,
);
} catch (e) {
Log.e("Error retrieving user or updating settings: $e");
}
}
settings_service.dart Here, updateUserSettings is defined
Future<void> updateUserSettings({
required String userId,
required bool randomPhotoOffset,
}) async {
// Create a new UserSettings object.
final settings = UserSettings(
userId: userId,
randomPhotoOffset: randomPhotoOffset,
);
try {
final request = ModelMutations.create(settings);
final response = await Amplify.API.mutate(request: request).response;
if (response.errors.isEmpty) {
Log.i("User settings updated: ${response.data}");
} else {
Log.e("Errors updating user settings: ${response.errors}");
}
} catch (e) {
Log.e("Exception updating user settings: $e");
}
}
schema.graphql This is the model definition required by Amplify
type UserSettings @model {
id: ID!
userId: String!
randomPhotoOffset: Boolean
}
Note: After I typed this model, I ran amplify push
and amplify codegen models
, which successfully generated `ModelProvider.dart' and 'UserSettings.dart'. I have not modified those in any way.
Attempt 1
I have tried using AI to solve this issue; however, its solutions are not viable
One example is updating my settings_service with this
Future<void> updateUserSettings({
required String userId,
required bool randomPhotoOffset,
}) async {
final settings = UserSettings(
userId: userId,
randomPhotoOffset: randomPhotoOffset,
);
try {
final request = ModelMutations.create(
settings,
modelProvider: ModelProvider.instance,
);
final response = await Amplify.API.mutate(request: request).response;
if (response.errors.isEmpty) {
print("User settings updated: ${response.data}");
} else {
print("Errors updating user settings: ${response.errors}");
}
} catch (e) {
print("Exception updating user settings: $e");
}
}
But ModelMutations.create does not have a modelProvider parameter available. I still think the issue lies in main.dart
Attempt 2
I have also tried changing configureAmplify() to this
void _configureAmplify() async {
try {
final authPlugin = AmplifyAuthCognito();
final apiPlugin = AmplifyAPI();
Amplify.addPlugins([authPlugin, apiPlugin]);
await Amplify.configure(amplifyconfig);
setState(() {
_amplifyConfigured = true;
});
} catch (e) {
print("Amplify configuration failed: $e");
}
}
Even though no syntax errors arise, the error above still occurs
Overview
My goal is to make a Flutter ios app with AWS. I am facing an issue when adding a persistent settings model to users, who will each have a corresponding settings model from their chosen items, like in any normal app.
More specifically, I want to have a single settings entry called randomPhotoOffset
, which is boolean. I was advised to use Amplify and DynamoDB to store user settings because it is scalable and offers more choices, compared to just using Cognito.
My workspace and versioning
- Using Amplify Gen 1 and CLI
- amplify_flutter: ^2.6.0
- amplify_auth_cognito: ^2.5.0
- amplify_api: ^2.6.0
- amplify_datastore: ^2.6.0
- Flutter sdk: ^3.5.4 I have updated my `pubspec.yaml' and my packages are all up to date and no versioning conflict occurs
Also testing on Simulator with device iPhone 16 Pro Mac on M4 Pro
The only issue
The only issue I am facing is an error I get when I toggle a button that changes one of my settings.
Exception updating user settings: ApiOperationException { "message": "No modelProvider found", "recoverySuggestion": "Pass in a modelProvider instance while instantiating APIPlugin" }
My code
main.dart This function is called after WidgetsFlutterBinding.ensureInitialized(); and before runApp().
Future<void> configureAmplify() async {
try {
final authPlugin = AmplifyAuthCognito();
final apiPlugin = AmplifyAPI();
final dataStorePlugin =
AmplifyDataStore(modelProvider: ModelProvider.instance);
Amplify.addPlugins(
[authPlugin, dataStorePlugin, apiPlugin],
);
await Amplify.configure(amplifyConfig);
// Start DataStore to register the model provider for GraphQL operations.
await Amplify.DataStore.start();
} on Exception catch (e) {
Log.e("Error configuring Amplify: $e");
}
}
settings.dart This is the only relevant function that is called once a button is pressed. Changes occur immediately after toggling, so a save button is avoided
Future<void> _toggleRandomPhotoOffset(bool newValue) async {
setState(() {
_randomPhotoOffset = newValue;
});
try {
// Retrieve the current user's ID from Amplify Auth.
final authUser = await Amplify.Auth.getCurrentUser();
await updateUserSettings(
userId: authUser.userId,
randomPhotoOffset: newValue,
);
} catch (e) {
Log.e("Error retrieving user or updating settings: $e");
}
}
settings_service.dart Here, updateUserSettings is defined
Future<void> updateUserSettings({
required String userId,
required bool randomPhotoOffset,
}) async {
// Create a new UserSettings object.
final settings = UserSettings(
userId: userId,
randomPhotoOffset: randomPhotoOffset,
);
try {
final request = ModelMutations.create(settings);
final response = await Amplify.API.mutate(request: request).response;
if (response.errors.isEmpty) {
Log.i("User settings updated: ${response.data}");
} else {
Log.e("Errors updating user settings: ${response.errors}");
}
} catch (e) {
Log.e("Exception updating user settings: $e");
}
}
schema.graphql This is the model definition required by Amplify
type UserSettings @model {
id: ID!
userId: String!
randomPhotoOffset: Boolean
}
Note: After I typed this model, I ran amplify push
and amplify codegen models
, which successfully generated `ModelProvider.dart' and 'UserSettings.dart'. I have not modified those in any way.
Attempt 1
I have tried using AI to solve this issue; however, its solutions are not viable
One example is updating my settings_service with this
Future<void> updateUserSettings({
required String userId,
required bool randomPhotoOffset,
}) async {
final settings = UserSettings(
userId: userId,
randomPhotoOffset: randomPhotoOffset,
);
try {
final request = ModelMutations.create(
settings,
modelProvider: ModelProvider.instance,
);
final response = await Amplify.API.mutate(request: request).response;
if (response.errors.isEmpty) {
print("User settings updated: ${response.data}");
} else {
print("Errors updating user settings: ${response.errors}");
}
} catch (e) {
print("Exception updating user settings: $e");
}
}
But ModelMutations.create does not have a modelProvider parameter available. I still think the issue lies in main.dart
Attempt 2
I have also tried changing configureAmplify() to this
void _configureAmplify() async {
try {
final authPlugin = AmplifyAuthCognito();
final apiPlugin = AmplifyAPI();
Amplify.addPlugins([authPlugin, apiPlugin]);
await Amplify.configure(amplifyconfig);
setState(() {
_amplifyConfigured = true;
});
} catch (e) {
print("Amplify configuration failed: $e");
}
}
Even though no syntax errors arise, the error above still occurs
Share Improve this question edited Feb 13 at 20:15 VLAZ 29.1k9 gold badges62 silver badges84 bronze badges asked Feb 13 at 15:54 fadedpigeon_47fadedpigeon_47 5133 silver badges11 bronze badges1 Answer
Reset to default 0This issue was suprisingly fixed by changing the order in which plugins were added.
In main.dart
from
Amplify.addPlugins([authPlugin, dataStorePlugin, apiPlugin]);
To
Amplify.addPlugins([dataStorePlugin, authPlugin, apiPlugin]);
And the following commands were required for me
flutter clean
flutter pub get
flutter run -d iPhone
Don't know why this was never referenced or needed by Amplify as authPlugin has no requirements for dataStore
本文标签:
版权声明:本文标题:amazon web services - Why am I unable to create user settings in my flutter ios app using AWS Amplify? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741519206a2383077.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论