AmplifyDataStore "No modelProvider found" error in Flutter

30 views Asked by At

Trying to query some data with GraphQl from Amplify. But receiving the error:

Query failed: ApiOperationException {
"message": "No modelProvider found",
"recoverySuggestion": "Pass in a modelProvider instance while instantiating APIPlugin"
}

Here is how I initialize it:

Future<void> init() async {   
final datastorePlugin =   AmplifyDataStore(modelProvider: ModelProvider.instance);   
final amplifyAuthCognito = AmplifyAuthCognito(       
secureStorageFactory: AmplifySecureStorage.factoryFrom());   
final amplifyApi = AmplifyAPI();   
final amplifyStorage = AmplifyStorageS3();  
await Amplify.addPlugins([amplifyAuthCognito, amplifyStorage, datastorePlugin, amplifyApi]) 
.catchError((e) {     
log('Error Amplify addPlugins $e');   });   
try {     await Amplify.configure(amplifyconfig);   
} 
on AmplifyAlreadyConfiguredException {     
safePrint('Tried to reconfigure Amplify; this can occur when your app restarts on Android.');   
} on Exception catch (e) {     
safePrint('An error occurred configuring Amplify: $e');   }

here is how I try to query data method:

Future<void> queryItem() async {     
try {       
final request = ModelQueries.list(        
 Plan.classType       );       
final response = await Amplify.API.query(request: request).response;       
final queryItem = response.data;       print('queryItem ${queryItem}');       
if (queryItem == null) {         
safePrint('errors: ${response.errors}');       }     } 
on ApiException catch (e) {       
safePrint('Query failed: $e');     }   }

here is application logs when running the project:

running the project

I tried also to query data using such method:

Future<void> queryPlans() async {     
try {       
final posts = await Amplify.DataStore.query(Plan.classType);       
safePrint('Plans: $posts');     
} on DataStoreException catch (e) {       
safePrint('Something went wrong querying Plans: ${e.message}');     
}   }

I know there data is present, but I'm receiving an empty list;

1

There are 1 answers

0
Yaroslav On

I found the solution, It should be modified amplifyApi by adding ModelProvider.instance:

final amplifyApi = AmplifyAPI(modelProvider: ModelProvider.instance);