firebase - Flutter ListView.builder 在滚动时结结巴巴并跳到顶部

标签 firebase asynchronous dart flutter google-cloud-firestore

从列表的中途向上滚动会使页面跳到顶部。我正在使用 Flutter 和 Firestore,以及 StreamBuilder 来获取数据。

我试过改变滚动物理,设置占位符,但似乎没有帮助。

  StreamBuilder<QuerySnapshot>(
    // Create a stream listening to the posts collection
    stream: widget.firestore
        .collection('posts')
        .orderBy('sequence', descending: false)
        .snapshots(),
    builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
      // When we don't have data yet (!...hasData), display the text "Loading..."
      if (!snapshot.hasData) return const Text('Loading...');
      final int messageCount = snapshot.data.documents.length;

      // When data is availible, load
      return new ListView.builder(
        //padding: EdgeInsets.all(3.0),
        itemCount: messageCount,
        itemBuilder: (_, int index) {
          final DocumentSnapshot document = snapshot.data.documents[index];
          if (document["type"] == "standard")
            return StandardCard(widget.firestore, document.documentID);
          else if (document["type"] == "text")
            return TextCard(widget.firestore, document.documentID);
          else if (document["type"] == "video")
            return VideoCard(widget.firestore, document.documentID);
          else
            return Card(
              // Database is incorrect
              child: Center(
                child: Text("[Missing sufficient information]"),
              ),
            );
        },
      );
    },
  ),

当你向下滚动时它会平滑滚动,但在向上滚动时会猛拉到顶部。

这是一个独立的示例。

import 'dart:math';

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'ListView Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'ListView Demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  stream() async* {
    yield ObjectHasFuture();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: StreamBuilder(
            stream: stream(),
            builder: (BuildContext context, snapshot) {
              if (!snapshot.hasData) return const Text('Loading...');
              return ListView.builder(
                itemBuilder: (_, int index) {
                  return Card(
                    child: snapshot.data,
                  );
                },
              );
            }));
  }
}

class ObjectHasFuture extends StatelessWidget {
  data() async {
    await Future.delayed(Duration(seconds: Random().nextInt(2)));
    return Container(
      height: 250,
      color: Colors.green,
      child: Center(
        child: Text(Random().nextInt(10000).toString()),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: data(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) return const Text("Loading");

          return snapshot.data;
        });
  }
}

最佳答案

您是在 Debug模式还是 Release模式下执行此操作? Debug模式有时会显示在最终构建中消失的奇怪工件。

关于firebase - Flutter ListView.builder 在滚动时结结巴巴并跳到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54545467/

相关文章:

asynchronous - AWS Lambda : Async Calls outside handler (initialization section, 调用 lambda)

javascript - 使用servlet从/WEB-INF/异步加载javascript : general concern

types - 在声明局部变量时,最好在Dart中声明类型或只使用 `var`?

res(generated) 文件夹下的 Android Security : values. xml

firebase - 了解 Firebase orderByChild

c# - TAP的多任务处理

flutter - 找不到签名配置 'release'

string - Flutter:是否可以仅在传递给文本小部件之前在字符串中格式化(粗体、斜体等)?

android - FirebaseCrashlytics setUserId - 我们是否需要在每次应用启动时设置它

ios - 当我从下载文件夹中删除文件时,为什么出现错误“无法找到构建输入文件”?