text
stringlengths 1
372
|
---|
}, |
child: padding( |
padding: const EdgeInsets.all(10), |
child: Text('Row $i'), |
), |
), |
); |
} |
return widgets; |
} |
} |
<code_end> |
<topic_end> |
<topic_start> |
how do i update ListView’s dynamically? |
on android, you update the adapter and call notifyDataSetChanged. |
in flutter, if you were to update the list of widgets inside a setState(), |
you would quickly see that your data did not change visually. |
this is because when setState() is called, the flutter rendering engine |
looks at the widget tree to see if anything has changed. when it gets to your |
ListView, it performs a == check, and determines that the two |
ListViews are the same. nothing has changed, so no update is required. |
for a simple way to update your ListView, create a new list inside of |
setState(), and copy the data from the old list to the new list. |
while this approach is simple, it is not recommended for large data sets, |
as shown in the next example. |
<code_start> |
import 'dart:developer' as developer; |
import 'package:flutter/material.dart'; |
void main() { |
runApp(const SampleApp()); |
} |
class SampleApp extends StatelessWidget { |
const SampleApp({super.key}); |
// this widget is the root of your application. |
@override |
widget build(BuildContext context) { |
return MaterialApp( |
title: 'sample app', |
theme: ThemeData( |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), |
), |
home: const SampleAppPage(), |
); |
} |
} |
class SampleAppPage extends StatefulWidget { |
const SampleAppPage({super.key}); |
@override |
State<SampleAppPage> createState() => _SampleAppPageState(); |
} |
class _SampleAppPageState extends State<SampleAppPage> { |
List<Widget> widgets = []; |
@override |
void initState() { |
super.initState(); |
for (int i = 0; i < 100; i++) { |
widgets.add(getRow(i)); |
} |
} |
@override |
widget build(BuildContext context) { |
return scaffold( |
appBar: AppBar( |
title: const Text('Sample app'), |
), |
body: ListView(children: widgets), |
); |
} |
widget getRow(int i) { |
return GestureDetector( |
onTap: () { |
setState(() { |
widgets = list.from(widgets); |
widgets.add(getRow(widgets.length)); |
developer.log('row $i'); |
}); |
}, |
child: padding( |
padding: const EdgeInsets.all(10), |
child: Text('Row $i'), |
), |
); |
} |
} |
<code_end> |
the recommended, efficient, and effective way to build a list uses a |
ListView.Builder. this method is great when you have a dynamic |
list or a list with very large amounts of data. this is essentially |
the equivalent of RecyclerView on android, which automatically |
recycles list elements for you: |
<code_start> |
import 'dart:developer' as developer; |
import 'package:flutter/material.dart'; |
void main() { |
runApp(const SampleApp()); |
} |
class SampleApp extends StatelessWidget { |
const SampleApp({super.key}); |
// this widget is the root of your application. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.