Ad
How To Dynamically Update ListView.builder() In Flutter?
I want to render a ListView dynamically. If a value changes in the list, I have to manually hot reload the application using my IDE for the changes to apply. I have tried using StreamBuilder, but the syntax was so complex.
Here's my code:
ListView.builder(
physics: NeverScrollableScrollPhysics(),
itemCount: myMap.length,
shrinkWrap: true,
itemBuilder: (context, index) {
return myMap.values.elementAt(index) == true
? Container(
padding: EdgeInsets.only(left: 20, right: 20, top: 20),
child: Container(child: Text(myMap.keys.elementAt(index)))
: Container();
}),
My stateful widget:
import 'package:flutter/material.dart';
class NextWidget extends StatefulWidget {
NextWidget({Key? key}) : super(key: key);
@override
_NextWidgetState createState() => _NextWidgetState();
}
class _NextWidgetState extends State<NextWidget> {
@override
Widget build(BuildContext context) {
return Container(
child: MaterialButton(onPressed: () {
setState(() {
myMap[1] = 'Modified Value';
}
}, child: Text('Modify')),
);
}
}
Ad
Answer
you have two possible approaches:
- pass a function from
parent widget
to thechild widget
. then you can use it to change thestate
of parent directly:
// in parent widget define a function
void parentSetState(){
setState((){});
//also you can add your own code too
}
// then in child widget use this function
MaterialButton(onPressed: () {
setState(() {
myMap[index] = index * 2;
widget.parentSetState();
}
}, child: Text('Modify')),
- use state managements like
provider
. Personally, I prefer to use this approach. see this link
Ad
source: stackoverflow.com
Related Questions
- → How do you create a 12 or 24 mnemonics code for multiple cryptocurrencies (ETH, BTC and so on..)
- → Flutter: input text field don't work properly in a simple example..... where am I wrong?
- → Can I customize the code formatting of Dart code in Atom?
- → Is it possible to develop iOS apps with Flutter on a Linux virtual machine?
- → Display SnackBar in Flutter
- → JSON ObjectMapper in Flutter
- → Material flutter app source code
- → TabBarSelection No such method error
- → How do I set the animation color of a LinearProgressIndicator?
- → Add different routes/screens to Flutter app
- → Is there a way to get the size of an existing widget?
- → How to share a file using flutter
- → Is there an easy way to find particular text built from RichText in a Flutter test?
Ad