type 'Null' is not a subtype of type 'List<int>' in type cast in flutter

958

You can use onBackgroundImageError to catch error

 CircleAvatar(
                  radius: 40.0,
                  backgroundImage: NetworkImage(
                      sessionControl.currentUser.value.picture,
                  ),
                  onBackgroundImageError:  (exception,context) {
                print('${sessionControl.currentUser.value.picture} Cannot be loaded');
                print('Error msg : ${exception.toString()}');
              },
                  backgroundColor: Colors.transparent,
                ),

and you can check if the provided URL working or not :

CircleAvatar(
                  radius: 40.0,
                  backgroundImage: NetworkImage(
                      'https://via.placeholder.com/120',
                  ),
                  onBackgroundImageError:  (exception,context) {
                    print('${sessionControl.currentUser.value.picture} Cannot be loaded');
                  },
                  backgroundColor: Colors.transparent,
                ),
Share:
958
Admin
Author by

Admin

Updated on January 01, 2023

Comments

  • Admin
    Admin over 1 year

    I am having a strange problem. I don't think that there is any problem with the url of image. Problem happens when I restart the simulator

    Update It seems the the image is expired How can I catch this exception?

    I am just calling a network image

    return CircleAvatar(
                          radius: 40.0,
                          backgroundImage: NetworkImage(
                              sessionControl.currentUser.value.picture),
                          backgroundColor: Colors.transparent,
                        );
    

    Error log

    Launching lib/main.dart on iPhone 13 in debug mode...
    Xcode build done.                                           13.5s
    Connecting to VM Service at ws://127.0.0.1:57758/38wgT4iPnbk=/ws
    flutter: Check 1 : Is Amplify SetState set to true ? false
    [GETX] Instance "GetMaterialController" has been created
    [GETX] Instance "GetMaterialController" has been initialized
    flutter: Check 1 : Is Amplify SetState set to true ? true
    flutter: Session Sign In Status = true
    flutter: Check 1 : Is Amplify SetState set to true ? true
    [GETX] Instance "HomeController" has been created
    [GETX] Instance "HomeController" has been initialized
    flutter: Requested for pulling current user info
    [GETX] Instance "SessionController" has been created
    [GETX] Instance "SessionController" has been initialized
    flutter: Printing session control picture :
    flutter: Printing session control picture :
    flutter: Printing session control picture : https://dontak4d751b4dd50548f1b4819e95919c2d17220339-oxlnewenv.s3.ap-southeast-2.amazonaws.com/public/Aaronimage_picker_29FADB72-2761-4BB9-89B0-9EFA849305BA-33481-0000009746CE94EE.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=%2F20210930%2Fap-southeast-2%2Fs3%2Faws4_request&X-Amz-Date=20210930T195105Z&X-Amz-Expires=17999&X-Amz-SignedHeaders=host&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEHQaDmFwLXNvdXRoZWFzdC0yIkYwRAIgJGnlBRuLiHfiSRjwIH8XnKYWqrmIPMfdOcchb8P878sCIBmI362NDDXenQUm%B8i30UG9uy0kucILDuxd3RreSTpb5HBJyUtJrvViYzy45XuHWjJ1WKEDxeAZdiDHEZPubnkkcGkcj0jdcaXtBP2OpR1cPT5MTWyv1dJ7mobezeRWvteSVzygA4Ot%2Fe5Yrf2ARLA3JDiIenmaxX7%2FAhvOUb0ppL9BocsAv0wojTSsdPvvQooRLqr%2FSjzEpDrVkHf7QOI50im9eBlk%2F8dKKERfwjIP41dyv1Wo5hyIc%2BfUmsXml0nRnluB<…>
    flutter: Note: calling the network image
    
    ════════ Exception caught by image resource service ════════════════════════════
    The following _CastError was thrown resolving an image codec:
    type 'Null' is not a subtype of type 'List<int>' in type cast
    
    When the exception was thrown, this was the stack
    #1      NetworkImage._loadAsync
    <asynchronous suspension>
    (elided one frame from dart:async)
    Image provider: NetworkImage("https://dontak4d751b4dd50548f1b4819e95919c2d17220339-oxlnewenv.s3.ap-southeast-2.amazonaws.com/public/Aaronimage_picker_29FADB72-2761-4BB9-89B0-9EFA849305BA-33481-0000009746CE94EE.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=%2F20210930%2Fap-southeast-2%2Fs3%2Faws4_request&X-Amz-Date=20210930T195105Z&X-Amz-Expires=17999&X-Amz-SignedHeaders=host&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEHQaDmFwLXNvdXRoZWFzdC0yIkYwRAIgJGnlBRuLiHfiSRjwIH8XnKYWqrmIPMfdOcchb8P878sCIBmItfJeejulE3fb0a7FgzSkedEkb8CrRxq%2BgeT47%2BJlaWUzJm6rC4r1%2F8Bc7T9aDqtYmNy1xlfVKr2D776gLFlWc3uMbwWeK3QQIRgb%2F0dMv4%2Bam4Z763as4v65nrs26%2B9S9U0XuII3V4M7pXDIspQwj%2B9Mq6sTsFwbYx0DMeCjVI96WAJdxuACAKJIgHR9DVaIHWI77IogWieRJPIH12quhZ%2F4GZr%2BfWvh8OL8Ak8feHTtcy%2FEQndGukUianBFEv2Erl7fc7VLdkLUZK1D40wa8Qlk1uH6Kt%2B36H%2FQNEIYRd%2FpzoyvSMuuhtDJ%2FfkQyvRv7TvJbCDzwibKom34sZyjK5yVc%3D&X-Amz-Signature=9537750a0fe8d32358f5461f305539147cbba13a1d337ee34b44f263d926da43", scale: 1.0)
    Image key: NetworkImage("https://dontak4d751b4dd50548f1b4819e95919c2d17220339-oxlnewenv.s3.ap-southeast-2.amazonaws.com/public/Aaronimage_picker_29FADB72-2761-4BB9-89B0-9EFA849305BA-33481-0000009746CE94EE.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=%2F20210930%2Fap-southeast-2%2Fs3%2Faws4_request&X-Amz-Date=20210930T195105Z&X-Amz-Expires=17999&X-Amz-SignedHeaders=host&X-Amz-Security-Token=IQoJb3JpZ2luX2VjHBhOUPrHu25bSB2fUAlzezITPYaksWbJyrZD%2BTiUzRvD5T2CWSDifjtYYLJ9HVj06XupHwhNyNrAC3wiIG%2Fsy1c4%2FYIGKUQ%2BR8PO7le%2FyNNMVQdFGFJvzW9BBhgcwR6JQwAUpHYpFiWZ1ZeVmk0fTR%2FFU721Tc9M%2BsVdxhcDnZVOR4CtIMR5jbNKSRp3ambx75lgZJEXwQiKol0aSTDx6iGlKkKBPCucT4JlCu9m5wlAZSU8ejJbWbgWxxK6sP1rLVQTC2Erl7fc7VLdkLUZK1D40wa8Qlk1uH6Kt%2B36H%2FQNEIYRd%2FpzoyvSMuuhtDJ%2FfkQyvRv7TvJbCDzwibKom34sZyjK5yVc%3D&X-Amz-Signature=9537750a0fe8d32358f5461f305539147cbba13a1d337ee34b44f263d926da43", scale: 1.0)
    ════════════════════════════════════════════════════════════════════════════════
    

    If I update the profile pic then it loads but after sometime it doesn't with above error. Can't understand why pls help.

    • Nonik
      Nonik over 2 years
      looks like urls you are showing have expiration , maybe this is why after a while, i mages are not showing, urls looks like from aws, if you have control you can set your own expiration
    • Admin
      Admin over 2 years
      @Nonik Thank you , I am checking the documentation
    • Admin
      Admin over 2 years
      @Nonik Any way to catch this error?