【问题标题】:Flutter Listview freezes while it is loading thumbnail images using FutureBuilderFlutter Listview 在使用 FutureBuilder 加载缩略图时冻结
【发布时间】:2018-03-06 06:28:37
【问题描述】:

我正在用 Flutter 创建一个文件管理器用于学习目的。我刚开始学习flutter,所以对它了解不多。

我的问题是我想在文件夹中显示图像的缩略图,因为我正在使用 ListView 中的 FutureBuilder 调用以 android 本机代码编写的函数。它工作正常,但每当我打开一个包含大量图像的目录时,它都会冻结 UI 一段时间,即使异步调用缩略图,我也无法在这段时间内滚动它。

Listview的Me代码:-

@override
Widget build(BuildContext context) {
    return new ListView.builder(
      itemBuilder: (itemContext, i) {
        if (_filesList != null && i < _filesList.length) {
          String currDir = _filesList[i]['path'];
          return new ListTile(
            title: new Text(_filesList[i]['name']),
            leading: (_filesList[i]['isDir'] == "false")
                ? new FutureBuilder<Uint8List>(
                    future: _getThumbnail(currDir),
                    builder: (BuildContext context,
                        AsyncSnapshot<Uint8List> snapshot) {
                      switch (snapshot.connectionState) {
                        case ConnectionState.waiting:
                          return new Container(
                            decoration: new BoxDecoration(
                              color: Colors.brown,
                              borderRadius: new BorderRadius.circular(20.0),
                            ),
                            width: 30.0,
                            height: 30.0,
                          );
                        default:
                          if (snapshot.hasError)
                            return new Container(
                              decoration: new BoxDecoration(
                                color: Colors.brown,
                                borderRadius: new BorderRadius.circular(20.0),
                              ),
                              width: 30.0,
                              height: 30.0,
                            );
                          else {
                            return new Image.memory(snapshot.data);
                          }
                      }
                    },
                  )
                : new Container(
                    decoration: new BoxDecoration(
                      color: (_filesList[i]['isDir'] == "true")
                          ? Colors.yellow
                          : Colors.brown,
                      borderRadius: new BorderRadius.circular(20.0),
                    ),
                    width: 30.0,
                    height: 30.0),
            onTap: () {
              setState(() {
                _currentDir = currDir;
              });
              _getFilesList(currDir);
            },
          );
        } else {
          return null;
        }
      },
    );
  }

_getThumbnail() 的代码:-

Future <Uint8List> _getThumbnail(path) async {
    try {
      String thumbnailString =
          await channel.invokeMethod("getThumbnail", {"path": path});
      thumbnailString = thumbnailString.replaceAll('\n', '');
      Uint8List bytes = BASE64.decode(thumbnailString);
      return bytes;
    } on PlatformException catch (e) {
      print("Files Not Found $e{e.message}");
      return null;
    }
  }

【问题讨论】:

标签: android dart flutter


【解决方案1】:

尽管 Flutter 中的调用是异步的,但 Android 上处理 getThumbnail 的代码可能会阻塞 UI 线程,从而导致加载冻结或滚动不稳定。一种解决方案是使用AsyncTaskdoInBackground 中构建/获取缩略图,并在onPostExecute 中返回结果。

【讨论】:

    猜你喜欢
    • 2020-02-09
    • 2021-10-20
    • 2020-02-26
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 2020-09-28
    相关资源
    最近更新 更多