Skip to content

[Bug report] Wrong observe results with small list and fast scrolling #113

Closed
@nikita488

Description

@nikita488

Version

1.25.0

Platforms

Android

Device Model

Poco F6

flutter info

• Flutter version 3.24.3 on channel stable at D:\Dev\FlutterSDK\3.22.3
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 2663184aa7 (5 months ago), 2024-09-11 16:27:48 -0500
    • Engine revision 36335019a8
    • Dart version 3.5.3
    • DevTools version 2.37.3

How to reproduce?

Sometimes there are wrong observe results, when i have a list with small amount of items (let's say 50 items) and when currently i near end of the list, and trying to scroll REALLY fast so that it reaches end almost immediatly, I expect last item to have index 49, but in reality it is 41 because of how quick it reached the end of the list.

I managed to fix this by wrapping this line

in WidgetsBinding.instance.addPostFrameCallback, so it observes results after frame end (exactly as you did it for Web).

So I'm proposing to wrap this line in WidgetsBinding.instance.addPostFrameCallback like you do on Web OR adding bool var "immediate" to ObserverWidget constructor that defaults to true to simulate old behaviour, and wrap it with addPostFrameCallback when it is false.

Logs

I/flutter (31017): First visible: 0
I/flutter (31017): Last visible: 41

Example code (optional)

import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:scrollview_observer/scrollview_observer.dart';

void main() {
  runApp(const TestApp());
}

class TestApp extends StatelessWidget {
  const TestApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: const MainApp(),
      scrollBehavior: kIsWeb ? _WebScrollBehavior() : null,
    );
  }
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: ListViewObserver(
          triggerOnObserveType: ObserverTriggerOnObserveType.directly,
          onObserve: (result) {
            print('First visible: ${result.firstChild?.index}');
            print(
                'Last visible: ${result.displayingChildModelList.lastOrNull?.index}');
          },
          child: ListView.builder(
            itemCount: 50,
            itemBuilder: (context, index) => Text('$index'),
          ),
        ),
      ),
    );
  }
}

class _WebScrollBehavior extends MaterialScrollBehavior {
  @override
  Set<PointerDeviceKind> get dragDevices =>
      {PointerDeviceKind.touch, PointerDeviceKind.mouse};
}

Contact

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions