skip to Main Content

In flutter I am using ListViewBuilder the _numberOfAlbum count i get is 27 and I can see 27 items on the screen. When I scroll up and down suddenly the screen goes black and seems like it keeps on trying to render the data. The error I get:

The following _TypeError was thrown building FutureBuilder<Album?>(dirty, state: _FutureBuilderState<Album?>#d3ae7):
type 'Null' is not a subtype of type 'Album' in type cast

When the exception was thrown, this was the stack: 
#0      _MultiGallerySelectPageState._showAlbumsDialog.<anonymous closure> (package:imagepickerflutter/MultiGallerySelectPage.dart:159:35)
#1      _FutureBuilderState.build (package:flutter/src/widgets/async.dart:612:55)
#2      StatefulElement.build (package:flutter/src/widgets/framework.dart:5198:27)
#3      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5086:15)
#4      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5251:11)
#5      Element.rebuild (package:flutter/src/widgets/framework.dart:4805:7)
#6      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:5068:5)
#7      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5242:11)

my code for the item:

  _showAlbumsDialog(int index) {
Album? item;
return GestureDetector(
  onTap: () {
    if (item != null) {
      Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => AlbumGallery(
                channel: _channel,
                albumId: item?.id,
                imagesCount: item?.count)),
      );
    }
  },
  child: Card(
    elevation: 2.0,
    child: FutureBuilder(
      future: _getAlbums(index),
      builder: (context, snapshot) {
        item = snapshot?.data as Album;
        if (item != null) {
          return Container(
            child: Row(
              children: [
                Expanded(
                  flex: 1,
                  child: Container(
                    width: 40,
                    height: 200,
                    child: Image.memory(
                      item?.data ?? Uint8List(0),
                      fit: BoxFit.fitHeight,
                    ),
                  ),
                ),
                Expanded(
                  flex: 2,
                  child: Container(
                    padding: EdgeInsets.all(8.0),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(
                          item?.title ?? '',
                          style: TextStyle(
                              fontSize: 18,
                              fontWeight: FontWeight.bold,
                              color: Colors.black),
                        ),
                        SizedBox(height: 4),
                        Text(
                          item?.count.toString() ?? 'No text',
                          style: TextStyle(
                            fontSize: 14,
                            color: Colors.grey,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
            decoration: BoxDecoration(
              border: Border.all(
                color: Colors.black,
                width: 2,
                style: _isSelected(item?.id ?? '')
                    ? BorderStyle.solid
                    : BorderStyle.none,
              ),
            ),
          );
        }

        return Container();
      },
    ),
  ),
);


}

Code for the ListView Builder:

  Widget _buildAlbumsList() {
    return ListView.builder(
      shrinkWrap: true,
      itemCount: _numberOfAlbums,
      itemBuilder: (context, index) {
        return ListTile(title: _showAlbumsDialog(index));
      },
    );
  }

I tried setting the _numberOfAlbums to a static value still the same issue. I’m not able to figure out the issue. I’m new on flutter.

3

Answers


  1. Try adding optional here on the type casting

    item = snapshot?.data as Album?;
    
    Login or Signup to reply.
  2. When using the as operator in Flutter, it’s important to keep the following points in mind to ensure safe usage:

    Use the is operator for type checking as much as possible and only use the as operator for type conversion when the type is confirmed, to avoid type conversion errors.

    When using the as operator for type conversion, make sure that the object being converted is actually of the target type, otherwise a TypeError exception will be thrown. To avoid this, you can use the as? operator, which returns null instead of throwing an exception if the object being converted is not of the target type.

    When using the as operator for type conversion, make sure that the target type is non-nullable, otherwise a TypeError exception will be thrown. If the target type may be nullable, you can use the safe type conversion operator as?.

    Please try:

    item = snapshot?.data as Album?;
    
    Login or Signup to reply.
  3. Its because at some point of iteration snapshot.data is null not Album so you can drop another control like

    
    if (snapshot?.data.runtimeType != Album) { 
     return SizedBox.shrink()
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search