list - 如何刷新 ListView.builder flutter

标签 list listview flutter dart builder

将项目添加到列表时如何刷新我的 ListView.builder?

ListView.builder(
      itemCount: propList.length,
      scrollDirection: Axis.horizontal,
      shrinkWrap: true,
      itemBuilder: (context,index)=> Text(propList[index]),
    )

我的按钮是
FlatButton(onPressed: (){propList.add('text');}, child: Text('add'))

最佳答案

假设您已扩展 StatefulWidget ,您必须调用 setState每次你改变状态。前任:

setState(() { propList.add('text'); });

这是一个简单的例子:

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) => MaterialApp(home: MyList());
}

class MyList extends StatefulWidget {
  @override
  _MyListState createState() => _MyListState();
}

class _MyListState extends State<MyList> {
  List<String> propList = [];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('My List'),
      ),
      body: ListView.builder(
        itemCount: propList.length,
        scrollDirection: Axis.horizontal,
        shrinkWrap: true,
        itemBuilder: (context,index)=> Text(propList[index]),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => setState(() => propList.add('text')),
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

关于list - 如何刷新 ListView.builder flutter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59927528/

相关文章:

python - 处理多个列表

python - 创建一个具有未知键的字典并向其附加一个列表值?

python - 如何删除列表列表中的 "\n"(python)

android - 渲染问题 渲染期间引发异常 : color and position arrays must be of equal length

flutter - 'Wrap' 小部件 - 项目对齐

android - 加载 InterstitialAd 时 Flutter App 崩溃

python - 如何检查列表中的项目是否在另一个列表中?

android - 使用 ListView 和自定义适配器设置数据

带复选框的 Android ListView

flutter - 不带振铃的日期时间00:00:00.000