dart 滚动ListView.Builder时,Flutter屏幕变黑

r1wp621o  于 2023-06-19  发布在  Flutter
关注(0)|答案(3)|浏览(170)

在flutter中,我使用ListViewBuilder,我得到的_numberOfAlbum计数是27,我可以在屏幕上看到27个项目。当我上下滚动时,屏幕突然变黑,似乎它一直在试图渲染数据。我得到的错误:

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)

我的项目代码:

_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();
      },
    ),
  ),
);

}

ListView生成器的代码:

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

我试着将_numberOfAlbums设置为静态值,仍然是同样的问题。我不明白这个问题。我是新手。

yduiuuwa

yduiuuwa1#

在Flutter中使用as运算符时,请务必记住以下几点,以确保安全使用:
尽可能使用is运算符进行类型检查,只有在确认类型时才使用as运算符进行类型转换,以避免类型转换错误。
当使用as操作符进行类型转换时,请确保被转换的对象实际上是目标类型,否则将引发TypeError异常。为了避免这种情况,您可以使用as?运算符,如果被转换的对象不是目标类型,则返回null而不是引发异常。
当使用as操作符进行类型转换时,请确保目标类型不可为空,否则将引发TypeError异常。如果目标类型可能是可空的,则可以使用安全类型转换运算符as?.
请尝试:

item = snapshot?.data as Album?;
aij0ehis

aij0ehis2#

尝试在类型转换上添加optional here

item = snapshot?.data as Album?;
wz3gfoph

wz3gfoph3#

这是因为在迭代的某个点snapshot.data是null而不是Album,所以您可以删除另一个控件,如

if (snapshot?.data.runtimeType != Album) { 
 return SizedBox.shrink()
}

相关问题