Skip to main content

Posts

Showing posts from June, 2020

setState() called after dispose() - flutter

this is a common error which happens when setState is called and the view has not been rendered yet,  so to avoid those kidn of errors just before calling the setState alway call  Just check boolean property  mounted  of the state class of your widget before calling  setState() . if ( this . mounted ){ setState ((){ //Your state change code goes here }); } Or even more clean approach Override  setState  method in your  StatelfulWidget  class. class DateTimeButton extends StatefulWidget { @override void setState ( fn ) { if ( mounted ){ super . setState ( fn ); } } } and that's they you avoid those kind of errros

Doing a shopping car with Flutter Provider

Doing a shopping using provider in flutter is easy the magic in here is uing the Provider package it is a hard code example, so you can just adjust it for your specific needs,  in the first view, you will have 3 products for choosing Product 1, Product 2 and Product 3, each of them has got their prices hardcoded,  when you click n any product, you are taken to the products view in which you are going to be able to increase or decrease the amount of items to take now when you click on the shopping cart, you are taken to the view which  shows you all the items you selected on that last view you increase or decrease any item, as the code is kind of big, you can get it from github

Getting and caching images with flutter_advanced_networkimage

when you need to get images from Internet you can do it with this solution which caches the images and reties if it fails getting them https://pub.dev/packages/flutter_advanced_networkimage so when I am getting an image I do something like this: Widget getImage ( String urlImage) { return TransitionToImage ( image: AdvancedNetworkImage ( urlImage, loadedCallback: () { //print('It works!'); }, loadFailedCallback: () { //print('Oh, no!'); }, ), loadingWidgetBuilder: (_, double progress, __) => Align ( alignment: Alignment .center, // Align however you like (i.e .centerRight, centerLeft) child: CircularProgressIndicator (), ), fit: BoxFit .cover, placeholder: const Icon ( Icons .refresh), enableRefresh: true , ); }

Using package_info - pubspec.yaml - auto update flutter app

If you need to get the version code or the package name, use this package_info in your pubspec.yaml package_info : 0.4.1 take the current version from  https://pub.dev/packages/package_info if you neeed to use it before starting the whole application set this in you main.dart and call it from the begining of the initState, let's suppose you've got a mechanism to have control over your version code, and each time you deliver a new app version code you want your users are forced to update the app, so one of many solutions to do it would be doing and enpoint in your api which get you the current application version code which is going to be compared with the is taken from the installed mobile phone, if they math the app goes on, if they do not, then you force the user to be redirected to a view in which the user is forced to update the new app void getVersionCode () async { PackageInfo packageInfo = await PackageInfo . fromPlatform (); String versionName = packageIn

FLutter navigation pop to index 1 or to the Main view with the current State

it does matter the view number you've got opened and you want to get back to the main view without reloading or refreshing data, and if you do not use name routes you can use  Navigator . of ( context ). popUntil (( route ) => route . isFirst ); if you are using Provider to have data stored with the Navigator . of ( context ). popUntil (( route ) => route . isFirst ); you won't lose any data when you get to the main view

How to Restrict Access to a Specific HTTP Referer AWS S3 Bucket

if you want to restrict images, pdf or any content to be read just from an specific domain name do this creating a new policy for your S3 Bucket {     "Version": "2012-10-17" ,     "Id": "http referer policy example" ,     "Statement": [         {             "Sid": "Allow get requests originating from www.yourdomain.co and yourdomain.co." ,             "Effect": "Allow" ,             "Principal": "*" ,             "Action": "s3:GetObject" ,             "Resource": "arn:aws:s3:::9rswfqxn/*" ,             "Condition": {                 "StringLike": {                     "aws:Referer": [                         "https://www.yourdomain.co/*" ,                         "https://yourdomain.co/*"                     ]                 }             }         }     ] } no if the contr