Ionic 4, getting an error while using the loader
loading.present is not a function
Here is my code:
const loading = this.loadingController.create({
message: 'Loading',
});
loading.present();
This is because loadingController.create() is an asynchronous method and before you get the instance of HTMLIonLoadingElement in variable loading,
you are calling the loading/present() which is undefined for the moment.
So you need to wait until you get the instance of HTMLIonLoadingElement while calling loadingController.create()
How to solve it: Simple use aync/await
const loading = await this.loadingController.create({
message: 'Loading',
});
loading.present();
See we used await just after the = operator. So it makes sure next line to be executed only when call to loadingController.create is complete and variable loading is initialized.
NOTE: Don't forget to add async keyword to the function inside which you are using the loader code, as we are using await.
Related
This is what I tried so far.
Let's say result is a StreamSubscription.
This is my flutter file
try {
result.listen(
(event) async {
// This converts the JSON data
final news = NewsModel.fromJSON(jsonDecode(event));
// This saves the data to local database
await localDataSource.saveNews([news]);
},
onError: (e) {
debugPrint('$e');
},
);
} catch (e) {
debugPrint('$e');
}
this is my flutter test since I want to test if method localDataSource.saveNews() fails
await newsRepository.subscribe(); calls the try catch above
controller is a StreamController to add new data to the stream
news is a dummy data, it doesn't matter because whatever the localDataSource do it will throw a LocalDBException
also I am using Mockito https://pub.dev/packages/mockito to mock the localDataSource above
test(
'should handle fail save news method',
() async {
// arrange
when(mockLocalDataSource.saveNews(any)).thenThrow(LocalDBException());
// act
await newsRepository.subscribe();
controller.add(news)
// assert
},
);
As you can see I don't have any condition to pass the flutter test, but that's beyond the point as this flutter test already breaks the stream even if I have a onError on my listener.
if I use controller.addError(LocalDBException()) the onError works, but if I deliberately throw an exception from the method localDataSource.saveNews() it breaks the stream.
Given this context I want to know 2 things:
I want to know how to handle the error inside the onData of StreamSubscription, if a method / function throw an exception, as it ignores the onError if a method / function throw an exception inside the listener.
Is adding an error through addError() function the same as throwing an exception inside the stream?
I tried to figure out the reason, but it seems there is only one issue linked to github, it keeps displaying this warning, plz let me know the reason, thanks a lot!
onPressed: () async {
print('now trying to purchase');
_purchaserInfo = await Purchases.purchasePackage(widget.package);
print('purchase completed');
appData.isGoldWorm = _purchaserInfo.entitlements.all["all_features"].isActive;
print('is user pro? ${appData.isGoldWorm}');
}
Without seeing the entire code and warning it is hard. I see you are defining the _purchaseInfo with the await but you aren't calling it so it may seem to the function that you are using async but not await, and so you cant assign it correctly.
I am using flutter_typeahead for showing place suggestions to users as they search using a text field and fetching these suggestions from HERE Maps. flutter typeahead package has an asynchronous callback function that requires, a list of strings to be returned that would then be shown to the user. The problem is that HERE Map's search engine doesn't return the search results and instead takes its own callback function which is called with the suggestions. Here's an example of it to make it clear.
TypeAheadFormField(
suggestionsCallback: (pattern) async {
final taskHandle = _searchEngine.suggest(
TextQuery.withAreaCenter(pattern, centerCoords),
searchOptions,
(error, suggestions) {
// How can i return these suggestions back from the suggestionsCallback?
final suggestionStrings = _handleSuggestions(error, suggestions);
},
);
},
),
The taskHandle also doesn't provide any way to await the searchEngine so I basically have no way of knowing when the suggestions will be available to return them by using a global variable (storing the suggestions after the searchEngine completes its callback and then returning the stored suggestions from the suggestionCallback.
The HERE SDK notifies in the SuggestionCallback when suggestions are available. Inside the callback you can proceed with your app logic, and, e.g. call typeAhead:
_searchEngine.suggest(
TextQuery.withAreaCenter(
"piz", // User typed "piz".
centerGeoCoordinates),
searchOptions, (SearchError? searchError, List<Suggestion>? list) async {
// Suggestions have been provided by HERE SDK. Use the results ...
});
Instead of using global variables it's probably better to proceed inside the callback. However, if you want to make this call blocking, you can wrap it in an async method that returns a Future. When calling this method you can await the resuts.
I'm trying to change my app from stateful widgets to provider. I put a function in the provider class and for some reason, it keeps giving me this error when I try calling the function. Below is the code for it. Thanks.
`onTap: Provider.of<Infomation>(context).onTap(),`
This is the function in the provider class
'void onTap(){
currentIndex = index;
notifyListeners();
}'
You're actually calling the function and assigning the result to the onTap property.
Either you do it like:
onTap: Provider.of<Infomation>(context).onTap
without calling it, which assigns the function reference to it, or just call it inside like:
onTap: () {
Provider.of<Infomation>(context).onTap();
}
Try also to just keep a single reference of the extracted provider, since in the case that you may need it the provider again, you're gonna have to do that long thing again. Try doing at the top of the build method like:
var infoProvider = Provider.of<Infomation>(context, listen: false);
Then use the methods off of the infoProvider instance like infoProvider.onTap() and such.
I'm trying to run a flutter test where a widget displays an error page when the Future provided to it throws an error (via FutureBuilder).
However the line where I create the future seems to be making the test fail.
final futureError = Future.delayed(Duration(milliseconds: 20))
.then((value) => throw Error());
with the message
Failed to load "D:\Projects\flutter\....dart": Instance of 'Error'
Putting it inside the test function resolved the issue
testWidgets('...',
(WidgetTester tester) async {
await tester.runAsync(() async {
final futureError = Future.error('error');
// ...
(it was in the group method prior to this)