Skip to content

Possibly undocumented behaviour in ListView.builder #58371

Closed

Description

@k-e-l-p

It seems that ListView.builder only builds items until it's itemBuilder callback returns null, ignoring everything else afterwards. In other words, the itemBuilder callback must ALWAYS return a non-null Widget or else it will not display any meaningful data that may come after any uninitialized Widget. Is this intended behaviour? I think it would be better if itemBuilder just carried on until it reached itemCount

Here is a short program to illustrate this behaviour:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Listview bug?'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  List<String> items = List<String>();


  void addItemToList(){
    setState(() {
      items.add("test");
    });
  }

  void addIgnoredItemToList() {
    setState(() {
      items.add("ignored");
    });
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        actions: <Widget>[
          FlatButton(
            child: Text(
              "add item",
            ),
            onPressed: addItemToList,
          ),
          FlatButton(
            child: Text(
              "add ignored item",
            ),
            onPressed: addIgnoredItemToList,
          ),
        ],
      ),
      body: Center(
        child: ListView.builder(
          itemCount: items.length,
          itemBuilder: (context, index) {
            Widget output;

            if (items[index] == "ignored") {
              return output;
            } else {
              output = Text(items[index]);
              return output;
            }
          },
        ),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.clear),
        tooltip: 'Reset',
        onPressed: (){
          setState(() {
            items.clear();
          });
        },
      ),
    );
  }
}

Here is a video demonstrating this behaviour:
https://imgur.com/a/FBupWR5

Flutter --version:

Flutter 1.17.1 • channel stable • https://github.com/flutter/flutter.git
Framework • revision f7a6a7906b (3 weeks ago) • 2020-05-12 18:39:00 -0700
Engine • revision 6bc433c6b6
Tools • Dart 2.8.2

Activity

  1. added
    p: material_uimaterial_ui package in flutter/packages
    frameworkflutter/packages/flutter repository. See also f: labels.
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    on Jun 1, 2020
  2. iapicca commented on Jun 1, 2020

    @iapicca

    Hi @nojusr
    the issue issue is present with the latest master

    doctor
    [✓] Flutter (Channel master, 1.19.0-2.0.pre.214, on Mac OS X 10.15.4 19E287, locale en-GB)
        • Flutter version 1.19.0-2.0.pre.214 at /Users/nevercode/development/flutter_master
        • Framework revision 1d395c5e18 (17 hours ago), 2020-05-31 07:41:50 -0700
        • Engine revision 923687f0e7
        • Dart version 2.9.0 (build 2.9.0-11.0.dev 6489a0c68d)
    
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
        • Android SDK at /Users/nevercode/Library/Android/sdk
        • Platform android-29, build-tools 29.0.2
        • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 11.3.1)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Xcode 11.3.1, Build version 11C504
        • CocoaPods version 1.9.0
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 3.6)
        • Android Studio at /Applications/Android Studio.app/Contents
        • Flutter plugin version 45.1.1
        • Dart plugin version 192.7761
        • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)
    
    [✓] VS Code
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.10.2
    
    [✓] Connected device (3 available)
        • Pixel 3a   • 965AY0WP5C • android-arm64  • Android 10 (API 29)
        • Web Server • web-server • web-javascript • Flutter Tools
        • Chrome     • chrome     • web-javascript • Google Chrome 83.0.4103.61
    
    • No issues found!
    thank you for reporting this
  3. added
    P2Important issues not at the top of the work list
    on Jun 1, 2020
  4. goderbauer commented on Jun 3, 2020

    @goderbauer
    Member

    This is expected and documented in the lower levels, but we should also document that on ListView.builder.

  5. self-assigned this
    on Jul 2, 2020
  6. added this to the 1.21 - July 2020 milestone on Jul 2, 2020
  7. github-actions commented on Aug 19, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  8. locked as resolved and limited conversation to collaborators on Aug 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work listd: api docsIssues with https://api.flutter.dev/found in release: 1.19Found to occur in 1.19frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onwaiting for PR to land (fixed)A fix is in flight

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions